Linux – Setting an environment variable in csh

cshenvironment-variableslinuxshell

I have the following line at the first line in my script file:

#!/bin/sh

So I'm using csh.(?)

I wanto assign the output of the following to an environment variable:

echo $MYUSR | awk '{print substr($0,4)}'

I try:

set $MYVAR = echo $MYUSR | awk '{print substr($0,4)}'

But it doesn't work,
How can I do it? I want to do it in a sh file.

Best Answer

Your script should look like

 #!/bin/csh

 set MYVAR = `echo $MYUSR | awk '{print substr($0,4)}'`

 echo $MYVAR

I don't have a way to test this right now, let me now if it doesn't work.


If you've inherited the basis of your script from someone else, with the #!/bin/sh, then you have to find out if /bin/sh is really the bourne shell, or if it is a link to /bin/bash

You can tell that by doing

   ls -l /bin/sh /bin/bash

if you get back information on files where the size is exactly the same, the you're really using bash, but called as /bin/sh

So try these 2 solutions

   MYVAR=$(echo $MYUSR | awk '{print substr($0,4)}')
   echo $MYVAR

AND

   MYVAR=``echo $MYUSR | awk '{print substr($0,4)}``  
   echo $MYVAR

   # arg!! only one pair of enclosing back-ticks needed, 
   # can't find the secret escape codes to make this look exactly right.

in all cases (csh) included, the back-ticks AND the $( ... ) are known as command substitution. What every output comes from running the command inside, is substituted into the command line AND then the whole command is executed.

I hope this helps.