Users with a Bash Shell
Problem statement
List the users whose login shell is bash, using awk to split each line of a passwd file and test one field. Then print more details, sorted by user ID, and see how an exact test and a pattern test differ. This is the classic first awk problem in interviews, and the same "lines where column X is Y" shape solves a huge number of log and report tasks.
passwd
root:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologinasha:x:1001:1001:Asha Rao:/home/asha:/bin/bashravi:x:1002:1002:Ravi Kumar:/home/ravi:/bin/zshdeploy:x:1003:1003::/home/deploy:/usr/bin/bashci:x:998:998:CI runner:/home/ci:/bin/bashPrint, in this order:
- The names of users whose shell is exactly
/bin/bash. - The names of users whose shell ends in
bash, wherever it is installed. - For those users: UID, name and home folder, sorted by UID as a number.
- The number of the last field and the total number of lines, using
NFandNR.
Expected output:
== shell is exactly /bin/bash ==rootashaci== shell ends in bash ==rootashadeployci== UID, name and home, sorted by UID ==0 root /root998 ci /home/ci1001 asha /home/asha1003 deploy /home/deploy== built-in counters ==fields on line 1: 7lines: 6Hints
awk -F: splits on colons, so the shell is $7. $7 == "/bin/bash" is an exact test.Approach
Optimal: awk pattern and action
Covers: awk -F, fields $1 to $NF, NF, NR, END, exact tests ==, pattern tests ~, print with commas, sort -n.
Every awk program is "pattern { action }". awk reads one line at a time, splits it into fields, and for each line checks the pattern. If the pattern is true, it runs the action. That is the whole idea:
into $1 ... $7"]):::purple S --> P{{"pattern true?"}}:::blue P --> A["run the action
print $1"]:::green P --> N["skip the line"]:::gray classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px
| Part | Example | Means |
|---|---|---|
-F: |
split fields at : (default: spaces and tabs) |
|
| pattern | $7 == "/bin/bash" |
run the action only on lines where this is true |
| action | { print $1 } |
what to do with those lines |
| no pattern | { print $1 } |
run on every line |
| no action | $7 == "/bin/bash" |
print the whole matching line |
Fields and built-in counters. $1, $2 and so on are the fields. $0 is the whole line. NF (number of fields) is how many fields this line has, so $NF is always the last field. NR (number of records) is the current line number. An END { ... } block runs once, after the last line, which is where totals go.
Exact test or pattern test? Both are useful, and the difference matters:
yes"]:::green ~~~ E2["/usr/bin/bash
no"]:::red end subgraph PT["$7 ~ /bash$/"] direction TB P1["/bin/bash
yes"]:::green ~~~ P2["/usr/bin/bash
yes"]:::green end EX ~~~ PT classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px style EX fill:transparent,stroke:#2563eb,stroke-width:2px style PT fill:transparent,stroke:#059669,stroke-width:2px
$7 == "/bin/bash"is true only for that exact text. It missesdeploy, whose shell is/usr/bin/bash.$7 ~ /bash$/is true when field 7 matches the patternbash$, meaning "ends in bash". It finds both paths.!~means "does not match".
Pick on purpose: exact tests avoid surprises, pattern tests cover variations.
Printing several fields. print $3, $1, $6 prints the fields with a single space between them, because the comma inserts awk's output separator. Without commas, print $3 $1 glues them together. The output goes into sort -n, which sorts by the first number, the UID.
Walking through the code. The # Setup: lines only create the sample file, so skip past them.
$7 == "/bin/bash" { print $1 }printsroot,ashaandci.$7 ~ /bash$/addsdeploy.- The third command prints
UID name homeand sorts by UID, so0 rootcomes first andci(998) comes beforeasha(1001). NR == 1 { print "fields on line 1:", NF }prints 7, andEND { print "lines:", NR }prints the total.
Edge cases. A blank line has NF equal to 0, so tests on $7 are simply false and nothing is printed. Text values need double quotes inside the program: $7 == "/bin/bash", while the whole program sits in single quotes so the shell leaves $7 alone.
# Setup: create the sample passwd file in a fresh temporary folder
cd "$(mktemp -d)"
cat > passwd << 'DATA'
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
asha:x:1001:1001:Asha Rao:/home/asha:/bin/bash
ravi:x:1002:1002:Ravi Kumar:/home/ravi:/bin/zsh
deploy:x:1003:1003::/home/deploy:/usr/bin/bash
ci:x:998:998:CI runner:/home/ci:/bin/bash
DATA
echo "== shell is exactly /bin/bash =="
awk -F: '$7 == "/bin/bash" { print $1 }' passwd
echo "== shell ends in bash =="
awk -F: '$7 ~ /bash$/ { print $1 }' passwd
echo "== UID, name and home, sorted by UID =="
awk -F: '$7 ~ /bash$/ { print $3, $1, $6 }' passwd | sort -n
echo "== built-in counters =="
awk -F: 'NR == 1 { print "fields on line 1:", NF } END { print "lines:", NR }' passwdRecapThe whole problem in a few lines, for the night before
- Spot it: "lines where column X is Y", "users with shell Z"
- Idea:
awk -F: '$7 == "/bin/bash" { print $1 }': pattern picks lines, action prints fields - Cost: one pass, O(n) time, constant memory
- Trap: forgetting
-F:so fields split on spaces, or using double quotes around the program
Interview follow-ups
Also print each user's home folder, but only if it is under /home.
Combine two tests with
&&:awk -F: '$7 ~ /bash$/ && $6 ~ /^\/home\// { print $1, $6 }' passwd. The second pattern means "starts with /home/", with each slash escaped as\/because/ends an awk pattern. You could also writeindex($6, "/home/") == 1, which avoids the escaping. Both skiproot, whose home is/root.
Frequently asked questions
Inside double quotes, the shell would replace $7 with its own variable number 7, which is usually empty, before awk ever sees it. Single quotes pass the program to awk exactly as written. If you need a shell variable inside, pass it in with -v: awk -F: -v shell="$SHELL" '$7 == shell { print $1 }' passwd. Mixing quotes to splice variables in is a common source of bugs.
No, it is a small programming language with variables, arrays, loops, if and functions. You can put a longer program in a file and run awk -f report.awk data.txt. In practice, most DevOps use is one-liners and short programs of a few lines, like the ones on this page. When a program grows past a screen, a Python script is usually easier to maintain.
They are different programs that all run the awk language. Ubuntu and Debian install mawk as awk by default, which is very fast. RHEL-style systems and many others use gawk (GNU awk), which adds extras like strftime, asort and better Unicode support. Everything on this page works in both. If you use a gawk-only feature, say so, or call gawk by name.