-2

I am learning bash scripting and did some exercises on one PC, committed the files to my GitHub so I can continue from another device.

When I pulled the repo I tried my existing scripts and they didn't work as on the initial device (with WSL Ubuntu 20.04).

When running these from WSL Ubuntu 18.04 I am getting funny errors:

tymon@Tymon-PC:/mnt/f/Documents/GIT/Bash_course_udemy/Section2$ ll
total 0
drwxrwxrwx 1 tymon tymon 512 Feb 20 21:14 ./
drwxrwxrwx 1 tymon tymon 512 Feb 20 17:56 ../
-rwxrwxrwx 1 tymon tymon 157 Feb 20 21:19 conditionalStatement.sh*
-rwxrwxrwx 1 tymon tymon 237 Feb 20 17:56 conditionalWithElif.sh*
-rwxrwxrwx 1 tymon tymon  57 Feb 20 21:14 hi.py*
-rwxrwxrwx 1 tymon tymon 335 Feb 20 22:02 hostname.sh*
-rwxrwxrwx 1 tymon tymon  22 Feb 20 17:56 sleepy.sh*
-rwxrwxrwx 1 tymon tymon 276 Feb 20 17:56 variable.sh*
tymon@Tymon-PC:/mnt/f/Documents/GIT/Bash_course_udemy/Section2$ . hostname.sh 
: command not found
 !u are running this script on Tymon-PC
!ou are running this script on Tymon-PC
: command not found
Using back quote(`XVZ`) for variable instead:
!ou are running this script on Tymon-PC
 !u are running this script on Tymon-PC
tymon@Tymon-PC:/mnt/f/Documents/GIT/Bash_course_udemy/Section2$ 

I checked permissions look correct, the bash path in the script is the same, but the result of the script on my Desktop is having errors and missing letters.

Example of the code:

#! /bin/bash

SERVER_NAME=$(hostname)
echo "You are running this script on $SERVER_NAME !"
echo "You are running this script on ${SERVER_NAME}!"

SERV_NAME=`hostname`
echo "Using back quote(\`XVZ\`) for variable instead:"
echo "You are running this script on ${SERV_NAME}!"
echo "You are running this script on $SERV_NAME !"

When I try to run this in VSC in WSL bash I can replicate:

tymon@Tymon-PC:/mnt/f/Documents/GIT/Bash_course_udemy/Section2$ . hostname.sh
: command not found
 !u are running this script on Tymon-PC
!ou are running this script on Tymon-PC
: command not found
Using back quote(`XVZ`) for variable instead:
!ou are running this script on Tymon-PC
 !u are running this script on Tymon-PC
tymon@Tymon-PC:/mnt/f/Documents/GIT/Bash_course_udemy/Section2$

If I switch to GIT bash the script runs fine:

Tymon@Tymon-PC MINGW64 /f/Documents/GIT/Bash_course_udemy/Section2
$ . hostname.sh
You are running this script on Tymon-PC !
You are running this script on Tymon-PC!
Using back quote(`XVZ`) for variable instead:
You are running this script on Tymon-PC!
You are running this script on Tymon-PC !

My question is:

  • would the different versions of bash make the difference - I can see the bash has different versions between WSL and GIT bash: GIT Bash:
tymon@Tymon-PC MINGW64 /f/Documents/GIT/Bash_course_udemy/Section2
$ bash --version
GNU bash, version 5.2.12(1)-release (x86_64-pc-msys)
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

WSL bash:

tymon@Tymon-PC:/mnt/f/Documents/GIT/Bash_course_udemy/Section2$ bash --version
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
  • is it possible that the pushing and pulling to GitHub broke the scripts?

  • I have tried to replicate it in different terminals (WSL, GCP, Git bash)

  • Tried re-create scripts on the affected device

  • Checked if the bash path provided in these scripts is correct

  • Checked if running bash hostname.sh or ./hostname.sh works

  • Tested if removing space from shebang would make a difference

  • 1
    [Please do not upload images of input/code/data/errors.](//meta.stackoverflow.com/q/285551) – Gilles Quénot Feb 20 '23 at 21:59
  • 1
    `.` is an alias for the `source` command. So `. hostname.sh` sources the file `hostname.sh` in the current shell. Since the script file has the execute bit set, you want to run it as `./hostname.sh` or `bash hostname.sh`. – David C. Rankin Feb 20 '23 at 22:00
  • 3
    You have configured git to change line-endings to DOS-style but you need unix style. See, for example, https://stackoverflow.com/q/10418975/10971581 – jhnc Feb 20 '23 at 22:23
  • See ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) and ["How to convert Windows end of line in Unix end of line (CR/LF to LF)"](https://stackoverflow.com/questions/3891076/how-to-convert-windows-end-of-line-in-unix-end-of-line-cr-lf-to-lf) for more info. – Gordon Davisson Feb 21 '23 at 00:18
  • Note that you can get the the hostname in Bash by simply using the [$HOSTNAME](https://www.gnu.org/software/bash/manual/bash.html#index-HOSTNAME) special variable. – pjh Feb 21 '23 at 01:36
  • And no space for `#!/bin/bash` – Nic3500 Feb 21 '23 at 02:16
  • @pjh the problem is definitely that the git pull has converted unix line endings to dos line endings. `sed 's/$/\r/' – jhnc Feb 21 '23 at 16:16
  • @Nic3500 ... umm, the space in `#! /bin/bash` is perfectly fine -- though it looks wonky for my taste... see [Is space allowed between #! and /bin/bash in shebang?](https://unix.stackexchange.com/q/276751/197080) – David C. Rankin Feb 22 '23 at 08:54

1 Answers1

0

Problem resolved, EOL was the issue here:

tymon@CEL054-lukasz  ~/GIT/Bash_course_udemy/Section2 (master)
$ file hostname.sh
hostname.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators

The editor I used added CRLF to these files...

I still don't understand why it worked fine on the initial device WSL and other terminals (git bash and gpg terminal)...

Different EOL handling on these?