-1

I have created the following script just for learning bash script in wsl saving as Desktop.sh in nano

#!/bin/bash 
cd /mnt/c/Users/myusername/Onedrive/Desktop

when i am in the shell and type bash Desktop.sh

nothing happens

Any advice would be helpfull thans

Rinzwind
  • 309,379
Nikolaos
  • 1
  • 1
  • 2

1 Answers1

2

Your script, which you invoked with the command bash Desktop.sh worked perfectly, but you do not see any effect.

Why?

Because the script is run in a subshell.

Executing the script with bash Desktop.shwill start a subshell, and run any command within the subshell. Thus, the current directory is changed in the subshell, and only in the subshell. When the script stops, you return to your shell, where nothing has changed. The same happens if you made the script executable, them run it by typing its pathname. In that case, the shebang (#!/bin/bash) is read to determine which shell to load.

To see an effect, you instead need to perform the cd command in the current shell. You can do so by sourcing the script:

source Desktop.sh

or with a shortcut notation:

. Desktop.sh

Now, the effect is as if you would have typed the commands in the script on the prompt yourself.

vanadium
  • 97,564