1.7 KiB
Marking a script as executable
Sometimes, you may need to mark a script as an executable to run it. On Linux, this can be accomplished by chmod +x [FILE]
, where [FILE]
is the file you want to mark as executable. To unmark a file as executable, use chmod -x
The Shebang
There are various shells with their own language syntax (sh
, bash
, fish
, etc). Therefore, more complicated scripts will indicate a particular shell by specifying the absolute path to the interpreter as the first line, prefixed by #!
like this:
#!/bin/sh
echo "Hello, World!"
#!/bin/sh
means that this script can be executed by the binary located at /bin/sh
, and so the reader knows this is a sh
(ell) script.
Variables
In bash, variable assignment is done with the =
operator. Variables are conventionally named with SCREAMING_SNAKE_CASE
, and can be accessed by prefixing the variable name with a dollar sign ($
):
#!/bin/bash
# There may not be any spaces used during assignment, `A = B` means something different than `A=B`
# Assignment is done with the equal sign (=) operator:
MY_VARIABLE="Hi Mom!"
echo $MY_VARIABLE
Quotes
In Bash, different styles of quotes mean different things:
Quote | Description |
---|---|
Commands
Command | Description |
---|---|
exit [CODE] |
Make the script process exit, where [CODE] is the exit code the process will terminate with. |