-1

I was trying to prepend a string to the filename of a bunch of files. I read through several questions and all of them recommended different syntax for basically the same command, only one of which worked.

I'm using cmder as my console. I was trying to prepend crypto- to all files in the CWD and here's what I tried:

This answer recommended a syntax like:

for i in *.png ; do mv "$i" "crypto-$i" ; done

i was unexpected at this time.


This answer recommended a double percent sign %%i:

for %%i in *.png ; do mv "$i" "crypto-$i" ; done

%%i was unexpected at this time.


This answer recommended a single percent sign instead of dollar sign for i:

for %i in *.png ; do mv "$i" "crypto-$i" ; done

*.png was unexpected at this time.

I got further this time and noticed that in the answer, the filename was enclosed in brackets, so I tried:

for %i in (*.png) ; do mv "$i" "crypto-$i" ; done

mv "$i" "crypto-$i" ; done
mv: target 'done' is not a directory

Then I figured that the command should also be in brackets:

for %i in (*.png) ; do (mv "$i" "crypto-$i") ; done

done was unexpected at this time.

After that, I decided to try without ;:

for %i in (*.png) do (mv "$i" "crypto-$i")

(mv "$i" "crypto-$i" )
mv: cannot stat '$i': No such file or directory

And finally, I thought that since I used % to refer to i the first time, I should use % again later on and tried:

for %i in (*.png) do (mv "%i" "crypto-%i")

This variant worked and all files were renamed successfully.


My question is - why did the last command work and the others didn't? Why are there so many different syntaxes that appear to work only in certain occasions? What are those occasions? Why do you put brackets sometimes and sometimes not? Why do you put semicolons sometimes and sometimes not? What's the difference between $i, %i and %%i?

John1024
  • 97,609
  • 11
  • 105
  • 133
dodov
  • 4,065
  • 3
  • 25
  • 44
  • 3
    The answer you're referring to seems to be about Bash, and you don't seem to be using Bash. – Benjamin W. Apr 24 '19 at 16:06
  • 4
    Several of these (`%i`, `%%i`, `(*.png)`) are not with/for/relevant-to POSIX-compliant shells at all. You're mixing Windows cmd.exe syntax with UNIX-shell syntax. – Charles Duffy Apr 24 '19 at 16:06
  • 2
    Be sure you check the tagging on questions. Questions tagged only `shell` are supposed to be about POSIX-family shells unless a more specific tag is added. Questions about Windows cmd.exe should be tagged `cmd`. You can't expect to apply answers to questions tagged `cmd` or `batch` to any UNIX-y shell. – Charles Duffy Apr 24 '19 at 16:08
  • Well, as I said, I'm using [cmder](https://cmder.net/) so I basically don't know if I'm using shell, bash or something else. Is it some kind of mix? Where do I read some information about all that? It's such a mess in my head. – dodov Apr 24 '19 at 16:09
  • 2
    cmder is just a console / terminal emulator. You need to know what's running inside it. – melpomene Apr 24 '19 at 16:09
  • I tried to see what runs inside cmder. It appears to be `cmd.exe`. I can run Windows commands like `dir` and `mkdir`. But if it's CMD, why can I run bash (?) commands like `ls` and `mv` as well? How do I know if they're shell or bash commands? – dodov Apr 24 '19 at 16:15
  • 2
    `ls` and `mv` are not bash commands, they're programs. Apparently you have the Windows versions of those installed. What do you mean by "shell or bash"? Bash is a shell. – melpomene Apr 24 '19 at 16:31
  • So cmd, shell and bash are not tied to the commands (programs) they can execute, I.E. `ls`, `mv`, `dir`? Each of them can execute those programs as long as I have them installed? In that case, what's the difference between the three? The way you write the actual commands themselves? I think I answered my own question there, but still... am I right? – dodov Apr 25 '19 at 09:28
  • I read a bunch of stuff and tried to summarize it in my answer to this question. Let me know if I understood it correctly. – dodov Apr 25 '19 at 10:08

1 Answers1

1

The syntax is based on what shell you're using. Since cmder has bash, I though I was using bash. Instead, it was cmd.

Also, the programs you can run in a CLI are not determined by the type of shell it uses. You can run ls, mv and all other "bash" programs as long as you have them installed.

I thought you could run "Windows programs" in CMD and "Unix programs" in Bash. I thought the shells are coupled together with the programs they most frequently used for. That's not the case.

So to answer the question:

i and $i is the syntax for bash:

for i in *; do echo $i; done

According to this answer, %i is the syntax for cmd:

for /l %x in (1, 1, 100) do echo %x

...while %%i is the syntax for commands in a batch file:

for /l %%x in (1, 1, 100) do echo %%x

Sidenote: "shell" is not a command line interpreter. It's the word for a command line interpreter. Bash and CMD are shells.

So to clarify:

  • shell is a user interface for access to an operating system's services. It can be a command line interface (CLI) or a graphical user interface (GUI). CLI shells (bash, cmd) allow you to interact with your machine via commands. GUI shells (File Explorer on Windows, Finder on macOS, for example) allow you to interact with your machine via your mouse and other peripherals.

  • cmder is a console emulator that comes with Unix tools (programs) like ls, mv, etc. It runs a CLI shell and can execute the Unix tools it shipped with.

  • bash is a CLI shell made for the GNU Project.

  • cmd is a CLI shell made for Windows.

  • cmder can emulate bash, cmd or PowerShell, which are all shells.

dodov
  • 4,065
  • 3
  • 25
  • 44