简体   繁体   中英

UNIX, Assign value to a variable in C or KornShell (ksh)

When I use bash to run the following code, it will assign the value 5 to the var1.

var1=$(awk '$1>$3{ print "5"}' newfile2)
echo $var1

But when I use this same code in banana or something, it gives me error. Can someone please tell me if there is some other way I can write this code so I can run it using the C or KornShell (ksh) as well.

For C shell, use

set var=`....`

For bash/ksh

var1=$(awk '$1>$3{ print "5"}' newfile2)

Use backticks and the set command for csh.

set var1=`awk '$1>$3{ print "5"}' newfile2`
echo $var1

Please note that there are no spaces before and after the variable name. so,

var1=`ls`

is what you need. But, if you have

var = `ls` 

you will get errors.

So, your code should be:

var1=`awk '$1>$3{ print "5"}' newfile2`
echo $var1

Make sure you are in BASH shell, not C or TCSH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM