Search code examples
shellshcsh

Set variable on the same line as command


In sh programming I can do this:

dryrun="echo " ./myscript.sh

And script uses it as:

${dryrun}ls -l (as an example)

I can't seem to figure out how to call script in csh with the same effect.


Solution

  • An easy solution is to use the env command:

    env dryrun=echo ./myscript.sh
    

    This will work with pretty much any shell. The env command takes a list of var=value pairs and adds them to the environment of whatever command is at the end of the list. See man env for more information.