Shell Scripting: Tips and Tricks

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


Remove the “Last login” Message

When you open a new terminal on macOS, you see:

Last login: Mon Dec 26 10:30:00 on ttys000

To remove this message, create an empty .hushlogin file in your home folder:

touch ~/.hushlogin

This works on macOS and Linux.

String Manipulation

Remove First N Characters

Use cut to remove the first 10 characters from a string:

#!/bin/sh
original="my original string"
result=$(echo $original | cut -c11-)
echo $result  # outputs: string

The cut -c11- means “start from character 11 to the end”.

Remove Last N Characters

Reverse the string, cut, then reverse again:

#!/bin/sh
original="my original string"
result=$(echo $original | rev | cut -c11- | rev)
echo $result  # outputs: my orig

Wildcards

Use wildcards with commands like ls:

ls image*     # files starting with "image"
ls *image     # files ending with "image"
ls *image*    # files containing "image"

Output Redirection

Redirect command output to a file:

ls > filelist.txt        # stdout to file
ls 2> errors.txt         # stderr to file
ls &> output.txt         # both to file
ls >> filelist.txt       # append to file

Redirect stderr to stdout:

command 2>&1

Pipes

Send output of one command as input to another:

echo hello | wc          # count words
ls | grep ".txt"         # filter results
cat file.txt | sort      # sort lines

Command Chaining

command1 ; command2      # run both regardless of success
command1 && command2     # run second only if first succeeds
command1 || command2     # run second only if first fails

Customizing Bash

Bash uses different configuration files:

For login shells (servers, TTY):

  1. /etc/profile (system-wide)
  2. Then the first found: ~/.bash_profile, ~/.bash_login, or ~/.profile

For non-login shells (terminal apps):

  1. /etc/bash.bashrc (system-wide)
  2. ~/.bashrc

Common things to add to .bashrc:

# Custom prompt
PS1='\u@\h:\w$ '

# Aliases
alias ll='ls -al'

# Add to PATH
export PATH="$PATH:$HOME/bin"

# Environment variables
export EDITOR=vim

Getting Help

Use man for command manuals:

man ls
man grep

For Bash built-ins, use help:

help cd
help echo

Or for a quick reference, install tldr:

tldr ls

Lessons in this unit:

0: Introduction
1: Introduction to Shells
2: Bash Basics
3: Writing Shell Scripts
4: Variables and Environment Variables
5: Loops and Arrays
6: Shell Script Functions
7: Creating Aliases
8: ▶︎ Tips and Tricks
9: The Fish Shell
10: Persist aliases and other configuration in Fish Shell
11: How to add a path to Fish Shell
12: Fish Shell, how to avoid recording commands to history
13: Fish Shell, how to remove the welcome message
14: How to replace all filenames with space with underscore using a shell script
15: How to update all npm packages in multiple projects that sit in subfolders