Results 1 to 4 of 4
hello people,
I'm really new here this is the first try to get some knowledge here.
I have a littele question.
"echo {1,2}" does expands 1 and 2.
so does ...
- 01-13-2011 #1Just Joined!
- Join Date
- Jun 2010
- Posts
- 8
shell curly braces with double quotes does not expand as i expect
hello people,
I'm really new here this is the first try to get some knowledge here.
I have a littele question.
"echo {1,2}" does expands 1 and 2.
so does "echo "`echo {1,2}`"".
But when I do it with double quotes which is "echo "{1,2}"" it doesn't expand at all.
why does this happen? I have read man for bash but could not get any clue.
Please tell me if you have any idea.
Regards,
Yuki
----------------------------Code:-- the output i see -- sh-3.2# echo {1,2} 1 2 sh-3.2# sh-3.2# echo "`echo {1,2}`" 1 2 sh-3.2# sh-3.2# echo `echo {1,2}` 1 2 sh-3.2# sh-3.2# echo "{1,2}" {1,2} sh-3.2#Last edited by MikeTbob; 01-14-2011 at 12:08 PM. Reason: Added Code Tags
- 01-13-2011 #2Just Joined!
- Join Date
- Mar 2010
- Posts
- 79
From here:In everyday speech or writing, when we "quote" a phrase, we set it apart and give it special meaning. In a Bash script, when we quote a string, we set it apart and protect its literal meaning.
Quoting
But there sure is a better explanation.
Quoting rules in Bash are anything but easy or clear. Double or single quotes, variables involved: yes or no, and so on.
In general: If you use variables you will want to use double quotes to avoid trouble with spaces.
Hoping i am not plain wrong.
- 01-14-2011 #3Just Joined!
- Join Date
- Jun 2010
- Posts
- 8
hello tornow,
Thank you for your reply.
I see using double quotes (") is not required in this term because it doesn't contain
any white spaces which is {1,2}.
What i was trying to do is to expand some items in shell script like bellow.
-----------------------
-----------------------Code:#!/bin/bash COMMAND="/bin/rm" TARGET="{/tmp/test1,/tmp/test2}" ${COMMAND} ${TARGET}
This is just a example what i want to do, i know we can do the same with "for".
i am just curious about this.
butCode:${COMMAND} {/tmp/test1,/tmp/test2} does work.
${COMMAND} ${TARGET} does not work, the curly braces does not expand its content.
I'm guessing this is caused because the value for $TARGET has been set which should be {/tmp/test1,/tmp/test2} and it doesn't expand afterward because the value is being treated as a value in $TARGET, not the shell thing, without the shell special meanings.
ummm....
I know sometimes there are things that I do not need to know the exact reason why things work like they do.
Thanks anyway!!Last edited by MikeTbob; 01-14-2011 at 12:09 PM. Reason: Added Code Tags
- 01-14-2011 #4Just Joined!
- Join Date
- Mar 2010
- Posts
- 79
In your example i would create an array and loop over it:
The curly brackets i use from the command-line only:Code:#!/usr/bin/env bash FILES=(file0.txt file1.txt file2.txt) for i in "${FILES[@]}" do echo "$i" done exit 0
where it, expansion, does workCode:mv /etc/X11/{xorg.conf,xorg.conf_backup}


Reply With Quote