Assign command output to a shell variable

I am trying to assign the output of the cut command to a variable, however I ran into some strange problem. I am using tcsh shell.

$echo $0 tcsh 

This is the command I run:

 $set a=`cut -f2 -d' ' test.txt` Missing }. //This is the output I am getting 

Now the file is simple (well, this is not the file I was working on, but I reduced the problem to that.)

Test.txt:

 { {corner 

Here it is! This is a file. If I changed the file to this:

 { {corner} 

The statement works, but "a" gets the following value:

 $echo $a corner //Please note its not {corner} but corner 

Therefore, I think the shell is trying to execute {corner as a command, and has complained since its closing bracket is missing. Does anyone know why he is exhibiting this behavior? I understand that it should just assign the output of cut to a variable, but it looks like it resolves it recursively! Newbie

+6
source share
1 answer

You must wrap it with double quotes

 set a="`cut -f2 -d' ' test.txt`" 

The same goes for echo usage types

 echo "$a" 

Exit

 {corner 
+5
source

Source: https://habr.com/ru/post/912012/


All Articles