0
user= whoami
echo $user

cd /home/${user}

I unable to change directory by this method.

output

pbsh56@pbsh56:~/Documents$ source one.sh
pbsh56

pbsh56@pbsh56:/home$ 
John Kugelman
  • 330,190
  • 66
  • 504
  • 555
  • 1
    no space after `=` !, and use `$()` if you want to capture the output of a command. Start at https://www.shellcheck.net/ – Diego Torres Milano Mar 15 '22 at 03:56
  • You may also use the `$HOME` system variable holding the path for your use, For instance if your name is `"Joe"`, then `[ "$HOME" = /home/joe ]`. – David C. Rankin Mar 15 '22 at 05:29
  • [Notice no spaces after the equals sign.](https://stackoverflow.com/a/2314765/1765658) – F. Hauri Mar 15 '22 at 06:47
  • Before asking such a question, I recommend running your code with `set -x` turned on. You will see immediately where ther error is. – user1934428 Mar 15 '22 at 07:18

1 Answers1

-1

Spacing is important. Because of the space in your command, this doesn't do what you expect:

user= whoami

What that does is define an empty variable called user, and then calls the whoami command with that empty variable added to the environment. The "pbsh56" you see is from the whoami command, not from the echo.

What you want is

user=$(whoami)
echo $user
cd /home/$user
Tim Roberts
  • 34,376
  • 3
  • 17
  • 24