Split String in Shell and Get the Last Field: 5 Easy Ways

To split string in shell and get the last field, you do not need a loop. Parameter expansion does it in one step: ${path%/*} removes the last field and ${path##*/} keeps only the last field. The old version of this post used a character loop to find the last slash and then called basename. It works for the sample path, and fails or slows down in the cases below. All results come from Bash 5.2.37 and dash on Debian 13.

How the old script works

The script walks through the string one character at a time with for (( i=0; i < ${#one_path}; i++)), remembers the position of the last slash, cuts the string at that position with ${one_path:0:$last_slash}, and takes the last part with basename. For /www/huu/phan/com/xyz it printed the new path /www/huu/phan/com and the last field com, as the post says. Paths with spaces also worked: /www/with space/dir name/file gave the new path /www/with space/dir name and the last field dir name, because the variables were quoted.

Where the loop script fails to split string in shell

  • No slash in the string. The variable last_slash stays at -1, and ${one_path:0:-1} means "all but the last character". For the input noslash the script printed New path: [noslas] and used it as the last field.
  • Empty string. The script stopped its substring step with $last_slash: substring expression < 0, then printed empty values.
  • A path with one slash. /onlyone gave an empty new path and an empty last field. That one is correct for a root-level name, but the script gives no sign of it.
  • Running with sh. Under dash, the first loop line failed: old.sh: 4: Syntax error: Bad for loop variable, exit code 2.

A trailing slash (/www/huu/phan/com/) was handled: the script printed /www/huu/phan/com and com. Note that it treats the trailing slash as the last separator, so the result is the same as for the path without the last empty field.

Split string in shell: the loop script cuts the last character when there is no slash, fails under sh and is slower than parameter expansion
Edge cases and timing when you split string in shell with a loop or with parameter expansion.

Split string in shell with parameter expansion

p="/www/huu/phan/com/xyz"
new_path=${p%/*}        # /www/huu/phan/com
last_field=${new_path##*/}   # com

The same inputs through this version: the sample path gave the same result as the loop; noslash gave [noslash] for both values (nothing was cut); the trailing slash path and the path with spaces gave the same results as before; /onlyone gave an empty new path. No substring arithmetic is involved, so the negative-length error cannot happen. The two operators are described in the Bash manual under parameter expansion: % removes the shortest match from the end, ## removes the longest match from the start. They also work in dash.

Other ways to split string in shell

The lab compared six forms for the same path. All printed the last field xyz:

  • ${p##*/} and $(basename "$p") (the latter starts a process);
  • awk -F/ '{print $NF}';
  • rev | cut -d/ -f1 | rev;
  • IFS=/ read -r -a parts <<< "$p" and ${parts[-1]}, which also reports six fields (the first is empty because of the leading slash);
  • sed 's|.*/||'.

The same operators work for other delimiters. For s="user@host.example.com", ${s##*@} printed host.example.com, ${s%%@*} printed user and ${s##*.} printed com. For the IFS rules behind read -a, see the IFS post.

A reusable function to split string in shell

If you need this in several scripts, wrap it once and handle the empty input yourself:

last_field() {
  [ -n "$1" ] || { echo "last_field: empty string" >&2; return 1; }
  printf '%s\n' "${1##*"$2"}"
}

The function takes the string and the delimiter. In the test, last_field "/www/huu/phan/com/xyz" "/" printed xyz, last_field "user@host.example.com" "@" printed host.example.com, last_field "a.b.c.d" "." printed d, and a string without the delimiter came back whole (noslash). A string that ends with the delimiter returned an empty field (only a newline in the dump), which is the honest answer: the last field is empty. An empty input printed last_field: empty string and returned code 1. Decide for your own script whether a trailing delimiter should be stripped first with ${p%/}.

Speed: loop versus parameter expansion to split string in shell

The loop is slow because Bash runs it character by character. In the lab, 2000 runs of each script, including the start of the shell, took 6.678 seconds for the loop version and 3.463 seconds for the parameter-expansion version. Inside one shell the gap is larger: with a path of about 8000 characters, 20 rounds of the loop took 2.222 seconds, and 20 rounds of ${p%/*} took 0.001 seconds. For short paths you will not notice it, but long inputs or a loop over a big file make the difference visible.

Common mistakes when you split string in shell

  • Cutting with a length of -1 when no delimiter was found.
  • Using cut for the last field: it counts from the start, so use awk '{print $NF}', rev or parameter expansion.
  • Forgetting to quote the variable, so that a path with spaces falls apart.
  • Using a Bash-only loop in a script that runs with sh (see the portable shell scripts post).

Which way to split string in shell should you pick?

Use parameter expansion for a string that is already in a variable: it starts no process, works in Bash and dash, and was the fastest in the test. Use basename and dirname when the input is a real file path and you want their handling of trailing slashes. Use awk when the text comes from a pipe, one line at a time. Use read -a when you need several fields by index, and remember that the first field is empty for a leading delimiter. The loop of the old post is useful only to understand how the substring works; do not copy it into a script that other people run.

FAQ

How do I split string in shell by several delimiters? Use awk -F'[/:]' or set IFS to the set of characters before read -a. Only the single-delimiter forms were tested here.

How do I handle a string without the delimiter? With parameter expansion nothing special is needed: ${q##*/} returns the whole string, as the test showed for noslash.

Comments

Popular posts from this blog

How to Play Minecraft Bedrock Edition on Linux: A Comprehensive Guide for Tech Professionals

Linux desktop frozen? My 5 go-to tricks to try - before forcing a hard reboot

The Ultimate Guide: How to Set Up DXVK in Wine on Linux for Enhanced Gaming Performance