Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run bash scripts on WSL

I'm trying to run a script script.sh from an Ubuntu WSL. In the Ubuntu WSL, calling sh script.sh simply returns without doing anything. Am I doing something wrong? what is the correct way to run Bash scripts in WSL?

I can run the script from Cygwin, so I know it is valid. The EOL is set to Unix. I've set permissions to 777. I've tried running it both from within the WSL interactive shell, and as a command from PowerShell: bash script.sh; neither works. I've tried bash, sh, wsl, ./.

Script:

#!/bin/bash

echo "hello"

Result from WSL:

~/test$ bash script.sh
~/test$

Result from Cygwin:

$ bash script.sh
hello

~/AppData/<path to ubuntu home>/test
$
like image 552
laifs Avatar asked Sep 16 '25 02:09

laifs


1 Answers

I was unable to replicate this issue. I've submitted this as an answer in order to include code:

henry@TV:~
498$ cat <<, >script.sh
#!/bin/bash
echo hello
,

henry@TV:~
499$ bash script.sh
hello

edit: Please note that the #!/bin/bash shebang and .sh extension are unnecessary if you're going to call this directly with bash. Your chmod is only necessary if you're going to be calling it as an executable ./script, in which case the shebang is required.

like image 63
vintnes Avatar answered Sep 18 '25 16:09

vintnes