Re: [DNG] [OT] bash / quote weirdness
On Wed, 12 Jan 2022 01:10:11 +0100 Antony Stone wrote: > Double-quoting turns the string into a single token, and therefore > the parser sees the line as: > > token 1 = "unrar x" > token 2 = "$f" > > Without the double quoting, it's: > > token 1 = "unrar" > token 2 = "x" > token 3 = "$f" > > "unrar" is a command which can be executed (in this case with a > parameter of "x"), whereas "unrar x" is not a command. > > You can see much the same thing if you try: > > for f in one two three four > do > echo "$f" > done > > for f in "one two" "three four" > do > echo "$f" > done > > > Antony. Hallo Antony, thank you - and yes, of course, that makes perfectly sense... As I have a strict "no whitespace policy" for my filesystems, I just wouldn't ever have thought of a command to contain one - and am still somewhat dumbfounded ;-) Libre Grüße, Florian ___ Dng mailing list Dng@lists.dyne.org https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng
Re: [DNG] [OT] bash / quote weirdness
On Tue, 11 Jan 2022 18:52:10 -0500 william moss wrote: > Bash is taking the string in the double quotes as a single command; > this is well documented. If either the command or parameters have > spaces, you will have to use eval. Check the bash man page for > details. > > This will also usually work > X=$( "command and such" ) > due to the execute block. Hallo William, I am replying to the list to share the valid (tested) alternative. Thanks a lot! Libre Grüße, Florian ___ Dng mailing list Dng@lists.dyne.org https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng
Re: [DNG] [OT] bash / quote weirdness
On Tue, 11 Jan 2022 18:16:15 -0500 tempforever wrote: > I believe quoting $xcmd would instruct the shell to look for and > execute "unrar x" so unless you have an executable file named unrar\ > x within $PATH, it will fail. The same thing happens within a shell: > ~$ "unrar x" > bash: unrar x: command not found > ~$ Hallo tempforever, thank you for your quick and simple reply. Seems that I can go sleep now :-) The full script, an archive batch-extractor (zip and rar supported so far) with absolutely no rights reserved, is attached. libre Grüße, Florian extract2newdir.sh Description: application/shellscript ___ Dng mailing list Dng@lists.dyne.org https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng
Re: [DNG] [OT] bash / quote weirdness
On Wednesday 12 January 2022 at 00:08:38, Florian Zieboll via Dng wrote: > #!/bin/bash > for f in "$@" ; do > xcmd="unrar x" > $xcmd "$f" > done > > Can please somebody explain, why, if I double-quote the "$xcmd" > variable in line 4, the script fails with > > ./test.sh: line 4: unrar x: command not found Double-quoting turns the string into a single token, and therefore the parser sees the line as: token 1 = "unrar x" token 2 = "$f" Without the double quoting, it's: token 1 = "unrar" token 2 = "x" token 3 = "$f" "unrar" is a command which can be executed (in this case with a parameter of "x"), whereas "unrar x" is not a command. You can see much the same thing if you try: for f in one two three four do echo "$f" done for f in "one two" "three four" do echo "$f" done Antony. -- The lottery is a tax for people who can't do maths. Please reply to the list; please *don't* CC me. ___ Dng mailing list Dng@lists.dyne.org https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng
Re: [DNG] [OT] bash / quote weirdness
Florian Zieboll via Dng wrote: > Dear list, > > this im my 'test.sh': > > #!/bin/bash > for f in "$@" ; do > xcmd="unrar x" > $xcmd "$f" > done > > Can please somebody explain, why, if I double-quote the "$xcmd" > variable in line 4, the script fails with > > ./test.sh: line 4: unrar x: command not found > > ??? > > Commands without parameters resp. whitespace (e.g. xcmd="unzip") work > fine when double-quoted; a web search (including the "GNU Bash manual" > [1]) did not shed any light on this mystery... > > Thank you and libre Grüße, > Florian > > > > [1] https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html > ___ > Dng mailing list > Dng@lists.dyne.org > https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng I believe quoting $xcmd would instruct the shell to look for and execute "unrar x" so unless you have an executable file named unrar\ x within $PATH, it will fail. The same thing happens within a shell: ~$ "unrar x" bash: unrar x: command not found ~$ ___ Dng mailing list Dng@lists.dyne.org https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng
[DNG] [OT] bash / quote weirdness
Dear list, this im my 'test.sh': #!/bin/bash for f in "$@" ; do xcmd="unrar x" $xcmd "$f" done Can please somebody explain, why, if I double-quote the "$xcmd" variable in line 4, the script fails with ./test.sh: line 4: unrar x: command not found ??? Commands without parameters resp. whitespace (e.g. xcmd="unzip") work fine when double-quoted; a web search (including the "GNU Bash manual" [1]) did not shed any light on this mystery... Thank you and libre Grüße, Florian [1] https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html ___ Dng mailing list Dng@lists.dyne.org https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng