Bash Script 11: Internal Bash Builtins

Bash has a lot of builtins, or internal functions, you can call. While most of them are very similar to utility programs available, the distinction is that no additional process is spawned. It may not sound like much, but a linux operating system has a limit to the number of processes that can run. If a server happens to run out of processes, but you still have a terminal open, then you still have a way to work with the system.

I/O builtins

In previous bash script posts, I’ve used the echo function. Here are some examples of that and more:

# echo something to the screen
echo "something to the screen"

# read in from the keyboard, line-by-line
while read LINE;
do
  #Do some processing here
  echo "$LINE"
done

# read in file.txt, line-by-line
while read LINE < file.txt;
do
  #Do some processing here
  echo "$LINE"
done

# append to file.txt
echo "Appending a line to the end" >> file.txt

# echo a formatted line
COUNT=1
NAME="Barry"
printf "Hi %s! The count is %d" "$NAME" "$COUNT"

Filesystem builtins

Bash also provides many of the fs builtins used every day.

# Change directory
cd /dir/to/enter

# List dir contents
ls -l

# Print the current directory
pwd

There are many more bash builtins than I’ve listed here, but these are the most commonly used.