laifs
laifs

Reputation: 329

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
$

Upvotes: 4

Views: 14780

Answers (2)

repo
repo

Reputation: 756

I just spent like ...too much time setting up scheduler. Cause bashrc was not loaded. here is the solution: wsl.exe in scheduler and then in parameters: -e bash -ic "which node > looper.scheduler.log"

or just run cmd and wsl -e bash -ic "which node > looper.scheduler.log"

for some reason the profile was not loading.. i added .bash_profile and source ~/.bashrc though inside in /mnt/user default directory but dont know if that mattered

Upvotes: 0

vintnes
vintnes

Reputation: 2030

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.

Upvotes: 2

Related Questions