Skip to main content
Doh!
Source Link
Stephen Kitt
  • 482.7k
  • 60
  • 1.2k
  • 1.4k

Remember that parameter expansion doesn’t happen magically, you need a shell to do it for you.

var=3 /bin/echo `$var` 

I’m not sure what you’re trying to do with that one, back-ticks aren’t appropriate here (they execute a command and are replaced with its output).

var=3 bash -c "/bin/echo '$var'" 

doesn’t work because parameter expansion occurs before the assignment, so the current shell changes "/bin/echo '$var'" to /bin/echo '$var''' (assuming var is undefined), and the child shell is run with

/bin/echo '' 

Try

var=3 bash -c 'echo $var' 

instead: the single quotes here protect the argument from the current shell, and the new shell is run with

echo $var 

which it will process as you’d expect.

In your second point, my initial comment still applies. If you want to play around with the environment and see what happens, run env and filter its output instead:

var=3 env | grep var var=3 exec env | grep var 

Remember that parameter expansion doesn’t happen magically, you need a shell to do it for you.

var=3 /bin/echo `$var` 

I’m not sure what you’re trying to do with that one, back-ticks aren’t appropriate here (they execute a command and are replaced with its output).

var=3 bash -c "/bin/echo '$var'" 

doesn’t work because parameter expansion occurs before the assignment, so the current shell changes "/bin/echo '$var'" to /bin/echo '$var' (assuming var is undefined), and the child shell is run with

/bin/echo '' 

Try

var=3 bash -c 'echo $var' 

instead: the single quotes here protect the argument from the current shell, and the new shell is run with

echo $var 

which it will process as you’d expect.

In your second point, my initial comment still applies. If you want to play around with the environment and see what happens, run env and filter its output instead:

var=3 env | grep var var=3 exec env | grep var 

Remember that parameter expansion doesn’t happen magically, you need a shell to do it for you.

var=3 /bin/echo `$var` 

I’m not sure what you’re trying to do with that one, back-ticks aren’t appropriate here (they execute a command and are replaced with its output).

var=3 bash -c "/bin/echo '$var'" 

doesn’t work because parameter expansion occurs before the assignment, so the current shell changes "/bin/echo '$var'" to /bin/echo '' (assuming var is undefined), and the child shell is run with

/bin/echo '' 

Try

var=3 bash -c 'echo $var' 

instead: the single quotes here protect the argument from the current shell, and the new shell is run with

echo $var 

which it will process as you’d expect.

In your second point, my initial comment still applies. If you want to play around with the environment and see what happens, run env and filter its output instead:

var=3 env | grep var var=3 exec env | grep var 
Reword.
Source Link
Stephen Kitt
  • 482.7k
  • 60
  • 1.2k
  • 1.4k

Remember that parameter expansion doesn’t happen magically, you need a shell to do it for you.

var=3 /bin/echo `$var` 

I’m not sure what you’re trying to do with that one, back-ticks aren’t appropriate here (they execute a command and are replaced with its output).

var=3 bash -c "/bin/echo '$var'" 

doesn’t work because parameter expansion occurs before the assignment, so the current shell changes "/bin/echo '$var'" to /bin/echo '$var' (assuming var is undefined), and the child shell is run with

/bin/echo '' 

and single quotes doesn't prevent parameter expansion in the current shell. Try

var=3 bash -c 'echo $var' 

instead: the single quotes here protect the argument from the current shell, and the new shell is run with

echo $var 

which it will process as you’d expect.

In your second point, my initial comment still applies. If you want to play around with the environment and see what happens, run env and filter its output instead:

var=3 env | grep var var=3 exec env | grep var 

Remember that parameter expansion doesn’t happen magically, you need a shell to do it for you.

var=3 /bin/echo `$var` 

I’m not sure what you’re trying to do with that one, back-ticks aren’t appropriate here (they execute a command and are replaced with its output).

var=3 bash -c "/bin/echo '$var'" 

doesn’t work because the child shell is run with

/bin/echo '' 

and single quotes doesn't prevent parameter expansion in the current shell. Try

var=3 bash -c 'echo $var' 

instead: the single quotes here protect the argument from the current shell, and the new shell is run with

echo $var 

which it will process as you’d expect.

In your second point, my initial comment still applies. If you want to play around with the environment and see what happens, run env and filter its output instead:

var=3 env | grep var var=3 exec env | grep var 

Remember that parameter expansion doesn’t happen magically, you need a shell to do it for you.

var=3 /bin/echo `$var` 

I’m not sure what you’re trying to do with that one, back-ticks aren’t appropriate here (they execute a command and are replaced with its output).

var=3 bash -c "/bin/echo '$var'" 

doesn’t work because parameter expansion occurs before the assignment, so the current shell changes "/bin/echo '$var'" to /bin/echo '$var' (assuming var is undefined), and the child shell is run with

/bin/echo '' 

Try

var=3 bash -c 'echo $var' 

instead: the single quotes here protect the argument from the current shell, and the new shell is run with

echo $var 

which it will process as you’d expect.

In your second point, my initial comment still applies. If you want to play around with the environment and see what happens, run env and filter its output instead:

var=3 env | grep var var=3 exec env | grep var 
added 2 characters in body
Source Link
Tim
  • 106.9k
  • 234
  • 651
  • 1.1k

Remember that parameter expansion doesn’t happen magically, you need a shell to do it for you.

var=3 /bin/echo `$var` 

I’m not sure what you’re trying to do with that one, back-ticks aren’t appropriate here (they execute a command and are replaced with its output).

var=3 bash -c "/bin/echo '$var'" 

doesn’t work because the child shell is run with

/bin/echo '' 

and single quotes doesn't prevent parameter expansion in the current shell. Try

var=3 bash -c 'echo $var' 

instead: the single quotes here protect the argument from the current shell, and the new shell is run with

echo $var 

which it will process as you’d expect.

In your second point, my initial comment still applies. If you want to play around with the environment and see what happens, run env and filter its output instead:

var=3 env | grep var var=3 exec env | grep var 

Remember that parameter expansion doesn’t happen magically, you need a shell to do it for you.

var=3 /bin/echo `$var` 

I’m not sure what you’re trying to do with that one, back-ticks aren’t appropriate here (they execute a command and are replaced with its output).

var=3 bash -c "/bin/echo '$var'" 

doesn’t work because the child shell is run with

/bin/echo 

and single quotes doesn't prevent parameter expansion in the current shell. Try

var=3 bash -c 'echo $var' 

instead: the single quotes here protect the argument from the current shell, and the new shell is run with

echo $var 

which it will process as you’d expect.

In your second point, my initial comment still applies. If you want to play around with the environment and see what happens, run env and filter its output instead:

var=3 env | grep var var=3 exec env | grep var 

Remember that parameter expansion doesn’t happen magically, you need a shell to do it for you.

var=3 /bin/echo `$var` 

I’m not sure what you’re trying to do with that one, back-ticks aren’t appropriate here (they execute a command and are replaced with its output).

var=3 bash -c "/bin/echo '$var'" 

doesn’t work because the child shell is run with

/bin/echo '' 

and single quotes doesn't prevent parameter expansion in the current shell. Try

var=3 bash -c 'echo $var' 

instead: the single quotes here protect the argument from the current shell, and the new shell is run with

echo $var 

which it will process as you’d expect.

In your second point, my initial comment still applies. If you want to play around with the environment and see what happens, run env and filter its output instead:

var=3 env | grep var var=3 exec env | grep var 
deleted 6 characters in body
Source Link
Tim
  • 106.9k
  • 234
  • 651
  • 1.1k
Loading
Typo.
Source Link
Stephen Kitt
  • 482.7k
  • 60
  • 1.2k
  • 1.4k
Loading
Source Link
Stephen Kitt
  • 482.7k
  • 60
  • 1.2k
  • 1.4k
Loading