Skip to main content
PacketMentor logo
Open menu
← All topics
Automation & Programmability Foundational

Bash for Network Engineers

The bash commands network engineers use every day: pwd, ls, cd, mkdir, rm, cp, mv, cat, echo, grep, wc, export, env. Each in one line, with an in-browser lab.

Quick summary
  • File management: ls, cp, mv, rm, touch, cat. Navigation: pwd, cd, mkdir. Env vars: export VAR=value, echo $VAR, unset VAR.
  • Redirects: > writes to a file (overwrite); >> appends. Combined with echo for one-liners.
  • Search inside files: grep <pattern> <file>. Count lines: wc -l <file>.

Mental model

Cisco objective 4.11 says “Utilize Bash commands (file management, directory navigation, and environmental variables)”. The core 15 commands cover 90% of every day shell work.

Why network engineers need bash:

  • Config pushes often ride ssh + a shell script.
  • Automation runners (CI/CD, Ansible, cron) run bash to glue steps together.
  • Any Linux-based network device management (Catalyst Center server, Ubuntu jump host, container shell) uses bash.

Directory navigation

CommandDoes
pwdPrint working directory
lsList files in current directory
ls -laLong listing including hidden files
cd <dir>Change into <dir>
cd ..Go up one directory
cd ~ or cdGo to your home directory
mkdir <dir>Create a directory
mkdir -p a/b/cCreate nested directories

File management

CommandDoes
touch <file>Create an empty file (or update timestamp if exists)
cat <file>Print the whole file
head -n 5 <file>First 5 lines
tail -n 5 <file>Last 5 lines
cp src dstCopy file
cp -r src dstCopy directory recursively
mv src dstRename or move
rm <file>Delete file
rm -rf <dir>Delete directory recursively (DANGEROUS; no undo)

Writing to files

PatternDoes
echo "hello" > file.txtCreate file.txt with “hello” (overwrites)
echo "another" >> file.txtAppend “another” to file.txt
cat file1 file2 > combined.txtConcatenate two files

Searching

CommandDoes
grep <pattern> <file>Lines containing pattern
grep -i <pattern> <file>Case-insensitive
grep -r <pattern> <dir>Recursive through a directory
wc -l <file>Count lines

Environment variables

Variables that live for the shell session (and inherited by child processes).

export MERAKI_KEY="abcdef0123456789"
echo $MERAKI_KEY
  • export VAR=value sets a variable and marks it exported so child processes (python, docker, ssh) see it.
  • echo $VAR prints it.
  • env lists every env var currently set.
  • unset VAR removes it.

Common env vars:

  • PATH — colon-separated list of directories bash searches when you type a command.
  • HOME — your home directory.
  • USER — your username.
  • PWD — current directory.

Persisting env vars

Add export lines to ~/.bashrc (or ~/.zshrc) to set them every time you open a shell.

.env files

A convention: put secrets in .env, add .env to .gitignore:

MERAKI_KEY=abcdef...
WEBEX_TOKEN=eyJhbG...

Load before running a script:

set -a; source .env; set +a
python my_script.py
Hands-on lab · 5 minute walkthrough

pwd, ls, cd, mkdir, echo, cat, cp, mv, export

12 steps against a real engine. Walk the directory tree, create files, grep, set an env var, read it back.

Open the lab →
bash-basics · lab
you@laptop:~$ export API_URL="https://api.example.com"
you@laptop:~$ echo $API_URL
https://api.example.com
you@laptop:~$

The #1 mistake

rm -rf with a typo. rm -rf / deletes everything on the system (modern systems try to guard against the literal / but variations slip through). rm -rf $UNSET_VAR/something where $UNSET_VAR expands to empty becomes rm -rf /something. Habits:

  • Prefer specific paths over glob patterns when deleting.
  • set -u at the top of a script fails loudly on an unset variable.
  • trash-cli as a safer alias on dev machines.

FAQ

bash vs zsh vs fish? bash is the baseline every Linux server has. zsh and fish are nicer interactive shells but your scripts should still target bash (shebang #!/bin/bash).

Why does my env var not survive a reboot? export only lasts for the current shell. Add it to ~/.bashrc (or system-wide /etc/environment) to persist.

What is set -euo pipefail? A common safety preamble in bash scripts: -e exits on first failing command, -u fails on unset vars, -o pipefail fails if any command in a pipe fails. Highly recommended.

I pressed Ctrl+S and the terminal froze. Ctrl+S is “stop output”; press Ctrl+Q to resume. Harmless but surprising if you have never hit it.

Master this on a real network

Want this drilled into reflex?

1:1 weekly sessions, live feedback on your labs, and US interview prep: built around the CCNA Automation® exam blueprint. Free first session. No card on file until you decide.

Claim my free session →

Get the free CCNA 12-week roadmap

You're already reading up on Bash for Network Engineers. The roadmap is the order I recommend studying every CCNA topic in: with what to lab each week and where Bash for Network Engineers fits. A written personal reply, not an autoresponder. Expect it within one business day.

Personal reply from a senior network engineer. No third-party tracking. Unsubscribe any time.