Linux
Bash Scripting
53 flashcards · answers and spaced-repetition review in the KnowCard app
Beyond just replaying a fixed command sequence, what capabilities make a bash script a real programming language rather than a glorified alias?
You wrote a script whose first line is #!/bin/bash. What is that line called, and what does the kernel actually do with it when you run the file?
Your script's shebang says #!/bin/sh instead of #!/bin/bash. On Debian and Ubuntu, why might a working script suddenly break under that shebang?
Inside a script, how do you reach the first argument the user typed on the command line, and why does this beat a plain alias?
You just wrote a new script file, but running ./myscript gives 'permission denied'. Which access bit is missing, and why does read access alone not fix it?
You're tired of typing ./ in front of your script every time. What has to be true so you can call it by bare name from anywhere, and where do you put it?
You want to drop a couple of lines into a new script file but don't want to open an editor. How can you do it with cat, and how do you signal you're done typing?
You want grep to show every configuration line that is NOT a comment. Which option flips grep's behavior, and which enables patterns joined with |?
In a regex, a line may start with any mix of spaces and tabs before the # you want to catch. Which POSIX class matches that whitespace, and what do ^ and $ anchor?
Your loop over filenames breaks apart a name like 'my report.tex' into 'my' and 'report.tex'. Which bash variable causes this, and how do you make it split only on newlines?
Inside a script that processes every file the caller passed, what does $* stand for, and how is it typically consumed?
You need the current two-digit month inside a backup filename. Which command produces it, and what format string extracts just the month?
You want to avoid writing a huge intermediate tarball to disk before uploading it. How does a pipeline let you stream tar output straight into an upload?
You want a script to read a text file one line at a time into a variable. What loop construct does this, and where does the file get attached?
You have a file listing database names, one per line, and want to run a command on each. How does 'for db in $(cat file)' work, and what's the hidden risk versus while read?
A script calls exit at the end. What does the numeric argument to exit mean, and what do 0, 1, and 2 conventionally signal?
A command in the middle of your bash script fails, but the script keeps going and does damage downstream. Is that a bug in your script, or expected bash behavior?
You want your script to stop the moment any command fails, instead of blundering onward. What do you add, and how do you make it apply to the whole script?
With set -e active, you run 'cmd1 || cmd2' and cmd1 fails. Does the script abort right there? At what point would it actually stop?
When bash runs a command in the foreground, what does it create for it, and which of the parent shell's variables does the new process actually receive?
Your script sets PATH, but after ./myscript finishes, the calling shell's PATH is unchanged. Why — and how do you run the script so its variable changes stick?
Bash predefines several read-only $ variables. What do $?, $!, $$, and $0 each report?
For analyzing command-line arguments in a script, what do $#, $1–$9, and $* / $@ give you?
Your script needs to handle more than nine positional parameters. What does shift do, and what does 'shift 9' accomplish?
Why is $$ the go-to ingredient for a temporary filename like tmp.$$, even when the same script runs in two terminals at once?
In bash, how do you access one element of an array versus all of them, and what's the syntax quirk to remember?
You assign y[abc]=123 expecting a string-keyed array, but everything lands in element 0. What did you forget, and what fixes it?
You want a default when a variable is empty. What's the difference between ${var:-default} and ${var:=default}?
A required variable might be unset and you want the script to fail loudly rather than continue with garbage. Which parameter-substitution form does that, and how do you get a string's length?
Given dat=/home/mk/book/book.tar.gz, how do ${dat#*/} and ${dat##*/} differ, and which one gives you the basename?
For dat=/home/mk/book/book.tar.gz, what do ${dat%pattern} and ${dat%%pattern} strip, and which gives the directory path?
You want to substitute text inside a variable's value. What's the difference between ${var/find/replace} and ${var//find/replace}?
You want to prompt the user and capture what they type into a variable. Which command reads the input, and how do you keep the prompt and the cursor on the same line?
What decides which branch of an if statement runs in bash, and why must the condition end with a semicolon when 'then' is on the same line?
A command returns exit code 0. In an if statement, does that count as true or false in bash — and why does this surprise programmers from other languages?
Writing 'if $a < 3' in a bash script doesn't compare numbers the way you'd expect. What must you use instead, and which operators compare integers?
How do you test that a file exists versus that a string is non-empty with the test command, and how can you inspect the result interactively?
You see conditions written as [ $a -eq 20 ] instead of test. What is the bracket form, and what silently-fatal mistake does it invite?
Bash also offers a [[ ... ]] condition form. What can it do that plain [ ] can't, and why can't you always use it?
You want to branch on several possible values of a variable, some matched by wildcards. How is a bash case statement structured, and how do you give it a default?
Bash has three loop keywords. What does each of for, while, and until loop on, and what do break and continue do?
How does a basic for loop over a list work in bash, and why should you quote "$file" when iterating over filenames?
You need a for loop over the numbers 1 to 12, sometimes with leading zeros or a step. What are the bash notations, and what arithmetic can't bash do?
How do you write a counting while loop in bash, and how can while consume the output of a pipeline line by line?
When would you reach for an until loop instead of a while loop, and how do you convert one into the other?
How do you define and call a function in bash, how are parameters passed, and what constraint on ordering must you respect?
Is the 'function' keyword required to define a bash function? What changes about the syntax if you leave it out?
You need to write several lines of literal text into a file or variable from inside a script. What syntax feeds a multiline block, and how does it know where to stop?
When storing a heredoc into a variable with read, what do the -r and -d '' options do, and why must you later echo it with quotes?
When bash executes a script, which of your shell configuration files (.bashrc, .profile, inputrc) does it load first?
You set the execute bit on a script, but typing its bare name in the folder where it lives still fails — while ./myscript works. Why won't bash run the bare name?
In a sed control line like s,athome,at home,g, what does the leading s do, and what does the trailing g change?
A bash script keeps running even after a command exits nonzero. So what kind of error is the exception that actually makes bash stop and refuse to continue?
Start learning today
Free to start — download the app or use it in your browser.
