What does #! operator mean in Linux? [duplicate] - shell

This question already has answers here:
How does the #! shebang work?
(3 answers)
Closed 5 years ago.
while following this tutorial I found a command
#!/bin/sh
git --work-tree=/var/www/domain.com --git-dir=/var/repo/site.git checkout -f
I don't understand the meaning of #!/bin/sh. I tried to search it but google removes the ! symbol from search results.
What does #!/bin/sh mean here?
Please help.

#! specifies the program with which the script should be executed if you not explicitly call it which any
in your case, if you call your script with: <scriptname.sh> Linux will execute it as /bin/sh <scriptname.sh>

Related

Find out what the (shell) script was invoked with [duplicate]

This question already has answers here:
How do I parse command line arguments in Bash?
(40 answers)
How to get exact command line string from shell?
(2 answers)
Closed 3 years ago.
Suppose my script.sh could take a number of options and arguments. What is the best way to find out what the script was invoked with (form inside the script)?
For eg., someone called it with script.sh --foo_option bar_arg
Is there a way to echo that exact command they typed from inside the script?
I've tried echo !! which does not work inside a script.

Why does this for loop run in terminal but not via script? [duplicate]

This question already has answers here:
How can I trigger brace expansion inside a script?
(2 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
I have a perfectly functional script
for i in {1..15}
do
amixer -D pulse sset Master 1%+;
done
But the command only runs once when i call it as ./volume.sh. If I copy and paste the code into the terminal, it runs fine. What's the difference?
I'm posting this answer because I found it very hard to find the solution anywhere else.
You need to add #!/bin/bash to the top of the script
#!/bin/bash
for i in {1..15}
do
amixer -D pulse sset Master 1%+;
done
This header hints the terminal to run this script as bash instead of something else. It's not just a decoration.

Why we use #!/bin/bash in Bash script? [duplicate]

This question already has answers here:
Why do you need to put #!/bin/bash at the beginning of a script file?
(10 answers)
What is the preferred Bash shebang ("#!")?
(6 answers)
Closed 4 years ago.
What is the significant of using #!/bin/bash in the starting of bash script? Can we write a bash script without #!/bin/bash ?
This line is called shebang. It’s a ‚magic‘ line telling the program loader (kernel) how to execute a script on unixoid systems.
Cf. https://en.m.wikipedia.org/wiki/Shebang_(Unix)

An alias defined in a bash script does not work within the same script [duplicate]

This question already has answers here:
How do create an alias in shell scripts? [duplicate]
(3 answers)
Closed 5 years ago.
I have a simple bash script.
alias myls=ls
myls
If I execute this script, I get an error.
$ bash foo.sh
foo.sh: line 2: myls: command not found
Why does the alias not work in the script?
Does this behavior conform to POSIX?
If it is indeed not supposed to work, could you please point me to an authoritative documentation that stays this?
See man bash:
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt

What is $? in bash? [duplicate]

This question already has answers here:
What are the special dollar sign shell variables?
(4 answers)
Closed 8 years ago.
I am trying to understand a script that was written a few years ago by someone that is no longer available here. The script references $? a few times. What is this?
P.S. Google couldn't help since it seems to strip the $? from the search term
You really should read the GNU bash manual. $? is a special parameter which
expands to the exit status of the most recently executed foreground pipeline.
Read also the advanced bash scripting guide.

Resources