Ravindra BagaleCourses & study guides मराठी Track your progress

Shell Scripting Complete 1: Your First Bash Script with Shebang, chmod, PATH and Exit Codes

A shell script is a text file full of commands that Linux runs one after another. Anything you type again and again at the prompt can live in a script. In this first chapter of Shell Scripting Complete you will write your first script, learn what the shebang line does, make the file executable with chmod +x, run it in four different ways, add comments, use exit codes, and debug it with bash -x. Every step shows the command, the output and what it means.

What you'll learn in this chapter

  • Write and save a first script
  • Understand the shebang line
  • Make a script executable with chmod +x
  • Run a script in different ways and know the difference
  • Use comments, exit codes and set -e
  • Debug with bash -n and bash -x

1. An everyday example: a recipe card

Why do we need this?

Aai writes her poha recipe on a card: wash poha, heat oil, add mustard seeds, add onion, add poha, add salt. Anyone can follow the card in the same order and get the same poha. A shell script is that card for the computer. The first line of the card says which language it is written in, so the cook knows how to read it.

What is it?

Recipe card = script file. Steps in order = commands line by line. Language note on top = shebang (#!/bin/bash). Permission to use the card = chmod +x. 'Done' or 'burnt' at the end = exit code 0 or not 0.

How to do it (step by step)

From idea to running scriptOpen a file in nano or vimWrite #!/bin/bash on line 1Add commands, one per lineSave the filechmod +x the fileRun it with ./name.shCheck $? for success
Write, make executable, run, check.

2. What is a shell script and why use one

Why do we need this?

Typing the same ten commands every morning is slow and you will make mistakes. A script does them the same way every time.

What is it?

A shell script is a plain text file. The shell (usually bash) reads it line by line and runs each line as if you typed it. Scripts can use variables, if, loops and functions, which you will learn in the next chapters. Files often end in .sh, but Linux does not need the extension.

How to do it (step by step)

  1. Which shell are you using?
echo $SHELL

Output:

/bin/bash

What it means: Your login shell is bash.

  1. Where is bash?
command -v bash

Output:

/usr/bin/bash

What it means: bash lives in /usr/bin (and /bin points there).

  1. Bash version.
bash --version | head -1

Output:

GNU bash, version 5.2.15(1)-release (x86_64-redhat-linux-gnu)

What it means: Bash 5 supports everything in this series.

3. Your first script: hello.sh

Why do we need this?

The fastest way to learn is to write one small script and run it.

What is it?

Create a file with a shebang line, an echo, and a date command. Save it. Then run it with bash. You do not need chmod to run with bash name.sh, because bash only reads the file.

How to do it (step by step)

  1. Create the file.
nano hello.sh

Output:

(editor opens)

What it means: Type the three lines below, then press Ctrl+O, Enter, Ctrl+X.

  1. The content.
#!/bin/bash
# my first script
echo "Namaskar from $(hostname)"
date

What it means: Line 1 is the shebang, line 2 is a comment, lines 3-4 are commands.

  1. Run it with bash.
bash hello.sh

Output:

Namaskar from web01
Sun Oct 25 10:05:11 IST 2026

What it means: Both commands ran in order.

4. The shebang line: #!

Why do we need this?

When you run ./script, Linux must know which program should read it. Without a shebang it guesses, and the guess can be wrong.

What is it?

The first two characters #! are called the shebang. After them comes the full path of the interpreter. #!/bin/bash means bash. #!/usr/bin/env bash finds bash in your PATH, which is good when bash lives in different places (Mac, BSD). #!/bin/sh means a basic POSIX shell (dash on Ubuntu), where bash features like [[ ]] and arrays may not work. It must be on line 1, with no spaces before it.

ShebangRuns withUse when
#!/bin/bashbash at a fixed pathLinux servers
#!/usr/bin/env bashfirst bash in PATHPortable scripts
#!/bin/shPOSIX sh (dash on Ubuntu)Very small, portable
#!/usr/bin/env python3python3Python scripts

How to do it (step by step)

  1. See the first line.
head -1 hello.sh

Output:

#!/bin/bash

What it means: The kernel reads this when you run ./hello.sh.

  1. What is /bin/sh here?
ls -l /bin/sh

Output:

lrwxrwxrwx 1 root root 4 Jan 10 /bin/sh -> dash

What it means: On Ubuntu sh is dash, not bash.

  1. A bash-only feature under sh fails.
sh -c '[[ 1 == 1 ]] && echo yes'

Output:

sh: 1: [[: not found

What it means: Use #!/bin/bash when you use bash features.

5. chmod +x: make the script executable

Why do we need this?

To run a script like a normal command (./hello.sh) Linux needs the execute permission. New files do not have it.

What is it?

chmod +x file adds execute for owner, group and others (limited by umask). chmod u+x adds it only for you. chmod 755 gives rwx to you and r-x to others. chmod 700 keeps it private. Check with ls -l: the x letters appear and many terminals show the name in green.

How to do it (step by step)

  1. Before.
ls -l hello.sh

Output:

-rw-r--r-- 1 asha asha 72 Oct 25 10:04 hello.sh

What it means: No x, so ./hello.sh will fail.

  1. Try to run.
./hello.sh

Output:

bash: ./hello.sh: Permission denied

What it means: Missing execute permission.

  1. Add x and run.
chmod +x hello.sh && ls -l hello.sh && ./hello.sh

Output:

-rwxr-xr-x 1 asha asha 72 Oct 25 10:04 hello.sh
Namaskar from web01
Sun Oct 25 10:06:40 IST 2026

What it means: Now it runs like a command.

  1. Private script.
chmod 700 db-backup.sh && ls -l db-backup.sh

Output:

-rwx------ 1 asha asha 410 Oct 25 10:07 db-backup.sh

What it means: Only you can read or run it. Good for scripts with passwords.

6. Four ways to run a script

Why do we need this?

Each way behaves a little differently. Knowing the difference explains why a cd or a variable sometimes 'does not stick'.

What is it?

1) ./name.sh: needs x and uses the shebang, runs in a new child shell. 2) bash name.sh: no x needed, ignores the shebang, child shell. 3) source name.sh or . name.sh: runs inside your current shell, so cd and variables stay after it ends. 4) Put it in a PATH folder like ~/bin or /usr/local/bin and run by name only.

WayNeeds x?New shell?
./name.shYesYes
bash name.shNoYes
source name.shNoNo, current shell
name.sh in PATHYesYes

How to do it (step by step)

  1. A script that changes folder.
cat gologs.sh

Output:

#!/bin/bash
cd /var/log
pwd

What it means: It moves to /var/log.

  1. Run normally.
./gologs.sh; pwd

Output:

/var/log
/home/asha

What it means: The cd happened in a child shell and was lost.

  1. Source it.
source gologs.sh; pwd

Output:

/var/log
/var/log

What it means: Now your own shell moved.

  1. Install in PATH.
mkdir -p ~/bin && cp hello.sh ~/bin/hello && hello

Output:

Namaskar from web01
Sun Oct 25 10:09:02 IST 2026

What it means: ~/bin is in PATH on most systems (log in again if not).

7. Comments and a good script header

Why do we need this?

In six months you will forget why a line is there. Comments explain it to you and to your team.

What is it?

Everything after # on a line is a comment (except the shebang on line 1 and # inside quotes). Start every script with a header: what it does, how to run it, the author and the date. Use blank lines between parts. For a long comment block you can use : <<'END' ... END.

How to do it (step by step)

  1. A header.
head -6 rotate-logs.sh

Output:

#!/bin/bash
# rotate-logs.sh - zip yesterday's app logs
# Usage: ./rotate-logs.sh
# Author: Ravindra Bagale, 2026-10-25

set -euo pipefail

What it means: Anyone can understand it before reading the code.

  1. Inline comment.
echo "start"   # tell the user

Output:

start

What it means: The comment is ignored when running.

  1. # inside quotes is text.
echo "Ticket #42 closed"

Output:

Ticket #42 closed

What it means: Inside quotes # is not a comment.

8. Exit codes and $?

Why do we need this?

Other scripts, cron and CI tools decide success or failure only by the exit code, not by the text printed.

What is it?

Every command returns a number from 0 to 255. 0 means success. Anything else means failure (1 general error, 2 misuse, 126 not executable, 127 command not found, 130 stopped by Ctrl+C). $? holds the code of the last command. Use exit N in a script to return your own code. && runs the next command only on success, || only on failure.

CodeMeaningExample
0Successls /tmp
1General errorgrep finds nothing
2Wrong usagels --bad
126Found but not executable./file without x
127Command not foundtypo like sl

How to do it (step by step)

  1. Success.
ls /etc/hosts; echo $?

Output:

/etc/hosts
0

What it means: 0 = worked.

  1. Failure.
ls /nope; echo $?

Output:

ls: cannot access '/nope': No such file or directory
2

What it means: Non-zero = failed.

  1. Your own code.
bash -c 'echo checking; exit 3'; echo "code=$?"

Output:

checking
code=3

What it means: Scripts can report their own status.

  1. Chain with && and ||.
ping -c1 -W1 10.0.0.9 && echo up || echo down

Output:

down

What it means: The ping failed, so only the || part ran.

9. Safer scripts: set -e, -u, -o pipefail

Why do we need this?

By default bash keeps going after an error. A failed cd followed by rm can delete the wrong files.

What is it?

set -e stops the script when a command fails. set -u stops when you use a variable that was never set (catches typos). set -o pipefail makes a pipe fail if any part fails, not only the last. Most teams write set -euo pipefail right after the shebang. You can switch an option off with set +e.

How to do it (step by step)

  1. Without -e.
bash -c 'cd /nowhere; echo still running'

Output:

bash: line 1: cd: /nowhere: No such file or directory
still running

What it means: Dangerous: the script went on.

  1. With -e.
bash -ec 'cd /nowhere; echo still running'; echo $?

Output:

bash: line 1: cd: /nowhere: No such file or directory
1

What it means: It stopped at the failed cd.

  1. -u catches typos.
bash -uc 'name=asha; echo $nmae'

Output:

bash: line 1: nmae: unbound variable

What it means: The typo is found at once.

  1. pipefail.
bash -c 'set -o pipefail; false | true; echo $?'

Output:

1

What it means: Without pipefail this would print 0.

10. Debug: bash -n, bash -x and set -x

Why do we need this?

When a script does something strange you need to see each line as it runs.

What is it?

bash -n file checks the syntax without running anything. bash -x file prints every command with + before it, after variables are expanded. Inside a script, set -x turns tracing on and set +x turns it off, so you can trace only one part. shellcheck (a free tool) finds common mistakes before you run.

How to do it (step by step)

  1. Syntax check.
bash -n broken.sh

Output:

broken.sh: line 9: syntax error: unexpected end of file

What it means: A missing fi or done. Nothing was run.

  1. Trace.
bash -x hello.sh

Output:

+ hostname
+ echo 'Namaskar from web01'
Namaskar from web01
+ date
Sun Oct 25 10:12:30 IST 2026

What it means: Lines with + are what bash really ran.

  1. Lint.
shellcheck rotate-logs.sh

Output:

In rotate-logs.sh line 12:
rm $dir/*.log
   ^--^ SC2086: Double quote to prevent globbing and word splitting.

What it means: Quote the variable. You will learn why in chapter 2.

11. Where to keep scripts and how to schedule them

Why do we need this?

A script that lives in Downloads gets lost. Scripts that should run daily should not depend on you remembering.

What is it?

Personal scripts: ~/bin. Shared scripts for all users: /usr/local/bin (needs sudo). Keep them in git. In cron and systemd timers there is a very small PATH and no terminal, so use full paths, set -euo pipefail and send output to a log file. The cron chapter has the details.

How to do it (step by step)

  1. Install for everyone.
sudo install -m 755 hello.sh /usr/local/bin/hello && which hello

Output:

/usr/local/bin/hello

What it means: install copies and sets the mode in one step.

  1. Run from cron every day at 6.
crontab -l

Output:

0 6 * * * /home/asha/bin/rotate-logs.sh >> /home/asha/rotate.log 2>&1

What it means: Full path and a log file.

  1. Check the log next day.
tail -3 ~/rotate.log

Output:

zipped 4 files
done

What it means: It ran without you.

12. Real examples from many industries

Why do we need this?

The same Linux skill shows up in every kind of company. Seeing it in many places helps you remember it.

What is it?

Each row is a made-up team. It shows who uses this and how.

How to do it (step by step)

Team and cityHow they use itCommand
Sneha, kirana chain, PuneDaily stock export script./export-stock.sh
Tom, bank, LondonPrivate scripts with chmod 700chmod 700 recon.sh
Yuki, gaming, TokyoPortable shebang on Mac and Linux#!/usr/bin/env bash
Fatima, hospital, Dubaiset -euo pipefail in backup jobsset -euo pipefail
Carlos, logistics, Mexico CityDebug a cron job with bash -xbash -x /opt/jobs/sync.sh
Arjun, ed-tech, BengaluruShared tools in /usr/local/binsudo install -m 755 t.sh /usr/local/bin/t

13. Common errors and how to fix them

Why do we need this?

Everyone hits these errors. Knowing the cause saves hours.

What is it?

Read the exact message first. Then match it to a row below.

How to do it (step by step)

Error you seeWhy it happensHow to fix
bash: ./hello.sh: Permission deniedNo execute permissionAdd it or run with bash.
chmod +x hello.sh
bash: hello.sh: command not foundCurrent folder is not in PATHUse ./ before the name.
./hello.sh
/bin/bash^M: bad interpreter: No such file or directoryWindows line endings (CRLF)Convert to Unix endings.
dos2unix hello.sh
bad interpreter: No such file or directoryWrong path in the shebangFix the path or use env.
head -1 hello.sh
[[: not foundRan with sh, not bashUse bash or a bash shebang.
bash script.sh
syntax error: unexpected end of fileMissing fi, done, } or quoteCheck pairs with bash -n.
bash -n script.sh
unbound variableset -u and a variable not set or misspeltFix the name or give a default.
echo "${name:-none}"
cd changes are lost after the scriptScript ran in a child shellUse source if you need it in your shell.
source gologs.sh
Works in terminal, fails in cronSmall PATH and no environment in cronUse full paths and log output.
command -v rsync

14. Troubleshooting step by step

Why do we need this?

Most first-script problems are permission, path, shebang or line endings.

What is it?

Go top to bottom. Stop at the first step that shows the problem.

How to do it (step by step)

  1. Is it executable?
ls -l script.sh
  1. Is the shebang right?
head -1 script.sh | cat -A
  1. Windows endings?
file script.sh
  1. Syntax ok?
bash -n script.sh
  1. Trace it.
bash -x script.sh
  1. Exit code?
./script.sh; echo $?
  1. Lint.
shellcheck script.sh

15. Lab 1: Write, chmod and run a server info script

Why do we need this?

Build sysinfo.sh that prints host, user, uptime and disk use, make it executable and install it in ~/bin.

What is it?

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

How to do it (step by step)

  1. Make a folder.
mkdir -p ~/lab37 && cd ~/lab37
  1. Write the script.
printf '#!/bin/bash\nset -euo pipefail\necho "Host: $(hostname)"\necho "User: $USER"\nuptime -p\ndf -h / | tail -1\n' | tee sysinfo.sh

Output:

#!/bin/bash ... (6 lines)
  1. Check syntax.
bash -n sysinfo.sh && echo OK

Output:

OK
  1. Try before chmod.
./sysinfo.sh

Output:

Permission denied
  1. Make executable.
chmod +x sysinfo.sh && ls -l sysinfo.sh

Output:

-rwxr-xr-x ... sysinfo.sh
  1. Run.
./sysinfo.sh

Output:

Host: web01
User: asha
up 3 days, 2 hours
/dev/xvda1 20G 6.1G 14G 31% /
  1. Exit code.
echo $?

Output:

0
  1. Install.
mkdir -p ~/bin && cp sysinfo.sh ~/bin/sysinfo && sysinfo | head -1

Output:

Host: web01

16. Lab 2: Exit codes, set -e and bash -x

Why do we need this?

See how a script behaves on error with and without set -e, and trace it.

What is it?

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

How to do it (step by step)

  1. A script with a bad cd.
printf '#!/bin/bash\ncd /no/such/dir\necho "I am in $(pwd)"\n' | tee risky.sh && chmod +x risky.sh
  1. Run.
./risky.sh; echo "code=$?"

Output:

cd: /no/such/dir: No such file or directory
I am in /home/asha/lab37
code=0
  1. Add set -e on line 2.
sed -i '2i set -e' risky.sh && head -3 risky.sh

Output:

#!/bin/bash
set -e
cd /no/such/dir
  1. Run again.
./risky.sh; echo "code=$?"

Output:

cd: /no/such/dir: No such file or directory
code=1
  1. Trace.
bash -x risky.sh

Output:

+ set -e
+ cd /no/such/dir
  1. Return your own code.
printf '#!/bin/bash\n[ -d /var/log ] || exit 4\necho ok\n' | tee chk.sh && bash chk.sh; echo $?

Output:

ok
0

Ravindra Bagale's Tip

Start every script with the same two lines: #!/bin/bash and set -euo pipefail. Then run bash -n before the first real run. These two habits catch most beginner mistakes before they damage anything.

17. Frequently asked questions

Q. Do I need the .sh extension?

No. It only helps humans.

Q. bash vs sh?

sh is basic POSIX; bash adds arrays, [[ ]] and more.

Q. Why ./ before the name?

The current folder is not in PATH for safety.

Q. chmod +x or chmod 755?

Both add execute; 755 also sets read for all exactly.

Q. What does source do?

Runs the file in your current shell.

Q. Can a script ask for sudo?

Yes, but better to run the whole script with sudo when needed.

Q. What is exit 0?

Finish and report success.

Q. How do I stop a running script?

Ctrl+C, or kill its PID.

18. Interview questions and answers

Beginner level

Q. What is a shebang?

The #! first line naming the interpreter.

Q. How to make a script executable?

chmod +x file.

Q. What does $? hold?

Exit code of the last command.

Q. What does exit code 0 mean?

Success.

Mid level level

Q. ./s.sh vs source s.sh?

Child shell vs current shell.

Q. Why #!/usr/bin/env bash?

Finds bash in PATH, portable.

Q. What does set -u do?

Error on unset variables.

Q. Code 126 vs 127?

Not executable vs not found.

Senior level

Q. Why set -o pipefail?

Pipes hide failures of earlier commands otherwise.

Q. A script works by hand but not in cron. Why?

Different PATH, no TTY, different env; use full paths and logs.

Q. How to debug a production script safely?

bash -n, shellcheck, set -x around the part, test copy.

Q. Bad interpreter with ^M?

CRLF endings; dos2unix or sed -i 's/\r$//'.

Try at home

  1. Write today.sh that prints the date, your user and the number of files in your home folder. Make it executable and run it.
  2. Break it on purpose (remove a quote) and find the error with bash -n.

Practice Tasks

About this scenario

Maple Leaf Books is a bookselling business in Toronto. Emily Tremblay looks after its IT and cloud work. They count money in CAD and run their AWS resources in the ca-central-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 21

Easy Make it runnable

Why: Small scripts save hours every week at BIZ.

What to do: Emily Tremblay at Maple Leaf Books wrote invoice-mail.sh. Give only the owner execute permission added.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Mode 755

Why: Small scripts save hours every week at BIZ.

What to do: Set nightly-sync.sh to rwxr-xr-x using numbers.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Run from here

Why: Small scripts save hours every week at BIZ.

What to do: Run the script stock-alert.sh that is in the current folder.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Run with bash

Why: Small scripts save hours every week at BIZ.

What to do: Run cleanup-tmp.sh with bash, without execute permission.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy All bash paths

Why: Small scripts save hours every week at BIZ.

What to do: Show every place bash is found, as type sees it.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy See the shebang

Why: Small scripts save hours every week at BIZ.

What to do: Print only the first line of deploy-web.sh.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Label the exit code

Why: Small scripts save hours every week at BIZ.

What to do: Print the last exit code with the label rc= in double quotes.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy What type

Why: Small scripts save hours every week at BIZ.

What to do: Show the content type of ship-orders.sh.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Check permissions

Why: Small scripts save hours every week at BIZ.

What to do: Long-list audit-users.sh to see its permissions.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Scripts bin

Why: Small scripts save hours every week at BIZ.

What to do: Create the folder ~/scripts/bin with parents.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy See PATH

Why: Small scripts save hours every week at BIZ.

What to do: Print your PATH variable.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Edit cron

Why: Small scripts save hours every week at BIZ.

What to do: Open your crontab for editing.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Source it

Why: Small scripts save hours every week at BIZ.

What to do: Load env-setup.sh into the current shell.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Syntax only

Why: Small scripts save hours every week at BIZ.

What to do: Check payroll.sh for syntax errors without running it.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Trace

Why: Small scripts save hours every week at BIZ.

What to do: Run report-gen.sh with every command printed before it runs.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Fix Windows line ends

Why: Small scripts save hours every week at BIZ.

What to do: Convert price-update.sh from Windows line endings.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Install to bin

Why: Small scripts save hours every week at BIZ.

What to do: Install backup-photos.sh into /usr/local/bin with mode 0755 using install.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Lint

Why: Small scripts save hours every week at BIZ.

What to do: Check fees-calc.sh with the popular linter for shell.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Another user cron

Why: Small scripts save hours every week at BIZ.

What to do: List the crontab of the user deploy with sudo.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Strict trace

Why: Small scripts save hours every week at BIZ.

What to do: Run sms-batch.sh with bash in both exit-on-error and trace mode.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Everyone run, only owner write

Why: Small scripts save hours every week at BIZ.

What to do: Remove write for group and others from tally.sh but keep execute for all, in one symbolic chmod.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

In short

  1. A script is a text file of commands run in order.
  2. Line 1 is the shebang, like #!/bin/bash.
  3. chmod +x lets you run ./name.sh.
  4. source runs in your current shell.
  5. 0 is success; check $?.
  6. set -euo pipefail and bash -x make scripts safe and easy to debug.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

First script in 4 steps. Line 1: the shebang. Write your commands below. Make it executable. Run it.
Revision card: First script in 4 steps · Download PNG · Open card page
Safe script header. Stop on errors. Fail on unset variables. Fail inside pipes too. Top of every script.
Revision card: Safe script header · Download PNG · Open card page
sh vs bash. POSIX, small, often dash. / Arrays, [[ ]], more features.
Comparison: sh vs bash · Download PNG · Open card page
./run.sh vs bash run.sh. Uses the shebang. / Ignores the shebang.
Comparison: ./run.sh vs bash run.sh · Download PNG · Open card page
Why the shebang line?. It picks the interpreter. Without it, the caller shell runs it. Portable form.
Interview card: Why the shebang line? · Download PNG · Open card page
Permission denied on ./x.sh?. The file is not executable. Or the disk is mounted noexec. Fix.
Interview card: Permission denied on ./x.sh? · Download PNG · Open card page
What is an exit code?. 0 means success. 1 to 255 means failure. See the last one.
Interview card: What is an exit code? · Download PNG · Open card page
Check a script before running?. bash -n checks syntax. shellcheck finds bugs. Run.
Interview card: Check a script before running? · Download PNG · Open card page

More cards: Ravi Sir's Cards.