I created a simple bash script, added the execute bit and moved it to /usr/bin to make is accessible by all Linux users:

user01@rhel10-vm4:~$ vim simplescript02
user01@rhel10-vm4:~$ ls -l simplescript02 
-rw-r--r--. 1 user01 user01 48 Oct 30 00:48 simplescript02
user01@rhel10-vm4:~$ chmod +x simplescript02 
user01@rhel10-vm4:~$ mv simplescript02 /usr/bin/
mv: cannot move 'simplescript02' to '/usr/bin/simplescript02': Permission denied
user01@rhel10-vm4:~$ sudo mv simplescript02 /usr/bin/

Displaying the content of the script with numbers of lines:

user01@rhel10-vm4:~$ nl /usr/bin/simplescript02
     1  #!/usr/bin/bash
     2  echo 'home dir is: '
     3  echo $HOME
user01@rhel10-vm4:~$ 

Executing the script:

user01@rhel10-vm4:~$ /usr/bin/simplescript02
home dir is: 
/home/user01
user01@rhel10-vm4:~$ 

I switched users to user02 and demonstrate that I can run the same bash script from its shell:

user01@rhel10-vm4:~$ sudo su user02
bash-5.2$ simplescript02
home dir is: 
/homeOnNFSsrv/user02
bash-5.2$ 

The shell had /usr/bin in its PATH variable. That is it executed the command despite I did not enter the full directory path.

bash-5.2$ echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin
bash-5.2$ 

Testing command substitution before including it in a bash script:

user01@rhel10-vm4:~$ $(id -Z)
bash: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023: command not found...
user01@rhel10-vm4:~$ echo $(id -Z)
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
user01@rhel10-vm4:~$ 

I created simplescript03 to display the current SELinux label:

user01@rhel10-vm4:~$ vim simplescript03
user01@rhel10-vm4:~$ 
user01@rhel10-vm4:~$ nl simplescript03
     1  echo 'the SELinux label of the current Linux user is:`
     2  echo `------------------`
     3  echo $(id -Z)
user01@rhel10-vm4:~$ 
user01@rhel10-vm4:~$ ls -l simplescript03 
-rw-r--r--. 1 user01 user01 95 Oct 30 01:04 simplescript03
user01@rhel10-vm4:~$ chmod +x simplescript03 
user01@rhel10-vm4:~$ sudo mv simplescript03 /usr/bin/
[sudo] password for user01: 
user01@rhel10-vm4:~$ ls
Desktop  Documents  Downloads  dummyfile  dummyfileDownloaded  localDir  Music  Pictures  Public  Templates  Videos  vim
user01@rhel10-vm4:~$ 
user01@rhel10-vm4:~$ simplescript03
/usr/bin/simplescript03: line 1: unexpected EOF while looking for matching `''
user01@rhel10-vm4:~$ 

Maybe the absence of the sheband is the cause of the error. I add it to the file and re-run the script:

user01@rhel10-vm4:~$ sudo vim /usr/bin/simplescript03
user01@rhel10-vm4:~$ nl /usr/bin/simplescript03
     1  #!/usr/bin/bash
     2  echo 'the SELinux label of the current Linux user is:`
     3  echo `------------------`
     4  echo $(id -Z)
user01@rhel10-vm4:~$ simplescript03
/usr/bin/simplescript03: line 2: unexpected EOF while looking for matching `''
user01@rhel10-vm4:~$ 

I took a close look at the second line. It seems I used the backticks instead of regular apostrophes. I change that and ran the script again:

user01@rhel10-vm4:~$ sudo vim /usr/bin/simplescript03
user01@rhel10-vm4:~$ nl /usr/bin/simplescript03
     1  #!/usr/bin/bash
     2  echo 'the SELinux label of the current Linux user is:'
     3  echo '------------------'
     4  echo $(id -Z)
user01@rhel10-vm4:~$ 
user01@rhel10-vm4:~$ simplescript03
the SELinux label of the current Linux user is:
------------------
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
user01@rhel10-vm4:~$ 

It worked. Now if I had a complex script, relying on stderr might not help much. So I test running the script in xtrace mode with bash -x:

user01@rhel10-vm4:~$ bash -x simplescript03
++ the SELinux label of the current Linux user is:
/usr/bin/simplescript03: line 2: the: command not found
+ echo

+ echo ------------------
------------------
++ id -Z
+ echo unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
user01@rhel10-vm4:~$ 

Lines with a ‘+’ or ‘++’ seem to be generated by the xtrace mode. But what is the difference between ‘+’ and ‘++’? #QA

references#

  • baeldung.com/linux/debug-bash-script
  • RHCSA RHEL10 Traning and Exam Preparation Guide, 4th ed, Ghori