2

I am making a simple shell script using the tcsh shell. I assigned a variable as a command to use later. But brackets within single quotes disappear.

set aaa = "test '10' '{}' ' " ; echo $aaa   

Result shows

test '10' '' ' 

Why does this happen? If I use {} and '' separately, it is ok.

set aaa = "test '10' {} '' ' " ; echo $aaa
test '10' {} '' '
1
  • 1
    Try echo "$aaa". I can't explain it, but it looks like tcsh is doing filename expansion after the variable substutution. May 21, 2022 at 16:32

1 Answer 1

1

I think it has something to do with csh doing brace expansion after parameter expansion. It does leave {} by itself alone, but it seems to remove the {} from e.g. foo{}. (I don't know why it doesn't just turn that into the empty string, even though there's basically an empty list between the braces. I think in other shells {} or {foo} with just one item inside doesn't trigger brace expansion.)

Quote the expansion to avoid that.

> set a = 'foo{a,b}'
> echo $a
fooa foob
> set b = '{}'
> echo $b
{}
> set c = 'foo{}'
> echo $c
foo
> echo "$c"
foo{}

And your particular case:

> set aaa = "test '10' '{}' ' "
> echo "$aaa"  
test '10' '{}' ' 
2
  • Thanks, it worked fine. But I have another problem. Actually I want to set the variable, aaa, using `` command. It's the example. set aaa = ` echo "test '10' '{}'" ` . In this case, the "" method can't use. I obtain and set the command from the pre-defined file. It has a lot of command like "awk". How can I resolve in this case?
    – aroman
    May 22, 2022 at 1:01
  • 1
    @aroman, dealing with the quoting hell with backticks in csh seems a different question which you might want to ask separately. (Or just change to a saner shell.)
    – ilkkachu
    May 22, 2022 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .