简体   繁体   中英

linux bash variable yields 2 different values depending on scope?

"$name" yields 2 different values: ?

#!/bin/bash

name=whoami
$name                                 # $name yields {user} being me is "jdl"

date >> /home/$name/crondate.txt      # $name yields "whoami"

Would like to have the path using "jdl"?

The value of name is whoami .

In your first example, it is not $name itself that yields jdl ; that's the output of the command whoami when it is executed.

If you were to run

echo "$name"

then you would see the value of the variable itself.

If you want to put jdl in name you should use:

name=$(whoami)

and if you want to print the value echo $name

actually what your code does:

name=whoami
$name                                 # replace name to whoami and execute it

date >> /home/$name/crondate.txt      # replace name to whoami and the path is /home/whoami/crondate.txt

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