Skip to main content
added 27 characters in body
Source Link
Gabriel Staples
  • 47.5k
  • 22
  • 251
  • 331
  1. Option 1 (will "break in mysterious ways", as @tripleee put it in a comment here, if the string stored in the A variable contains certain special shell characters, so Option 2 below is recommended instead!):
    # Capture space-separated words as separate elements in array A_array
    A_array=($A)
    
  2. Option 2 [RECOMMENDED!]. Use the read command, as I explain in my answer here, and as is recommended by the bash shellcheck static code analyzer tool for shell scripts, herein ShellCheck rule SC2206, here.
    # Capture space-separated words as separate elements in array A_array, using
    # a "herestring". 
    # See my answer here: https://stackoverflow.com/a/71575442/4561887
    IFS=" " read -r -d '' -a A_array <<< "$A"
    
  1. Option 1 (will "break in mysterious ways", as @tripleee put it in a comment here, if the string stored in the A variable contains certain special shell characters, so Option 2 below is recommended instead!):
    # Capture space-separated words as separate elements in array A_array
    A_array=($A)
    
  2. Option 2 [RECOMMENDED!]. Use the read command, as I explain in my answer here, and as is recommended by the bash shellcheck static code analyzer tool for shell scripts, here.
    # Capture space-separated words as separate elements in array A_array, using
    # a "herestring". 
    # See my answer here: https://stackoverflow.com/a/71575442/4561887
    read -r -d '' -a A_array <<< "$A"
    
  1. Option 1 (will "break in mysterious ways", as @tripleee put it in a comment here, if the string stored in the A variable contains certain special shell characters, so Option 2 below is recommended instead!):
    # Capture space-separated words as separate elements in array A_array
    A_array=($A)
    
  2. Option 2 [RECOMMENDED!]. Use the read command, as I explain in my answer here, and as is recommended by the bash shellcheck static code analyzer tool for shell scripts, in ShellCheck rule SC2206, here.
    # Capture space-separated words as separate elements in array A_array, using
    # a "herestring". 
    # See my answer here: https://stackoverflow.com/a/71575442/4561887
    IFS=" " read -r -d '' -a A_array <<< "$A"
    
added 1020 characters in body
Source Link
Gabriel Staples
  • 47.5k
  • 22
  • 251
  • 331

(Run each of these commands in your terminal to test this live.)

For all answers below, start withby typing this in your terminal:

Convert A to an array, with elements being separated by the default IFS (Internal Field Separator) char, which is space:

  1. Option 1 (will "break in mysterious ways", as @tripleee put it in a comment here, if the string stored in the A variable contains certain special shell characters, so Option 2 below is recommended instead!):
    # Capture space-separated words as separate elements in array A_array
    A_array=($A)
    
  2. Option 2 [RECOMMENDED!]. Use the read command, as I explain in my answer here, and as is recommended by the bash shellcheck static code analyzer tool for shell scripts, here.
    # Capture space-separated words as separate elements in array A_array, using
    # a "herestring". 
    # See my answer here: https://stackoverflow.com/a/71575442/4561887
    read -r -d '' -a A_array <<< "$A"
    

Then, print only the last elment in the array:

# Capture space-separated words as separate elements in array A_array
A_array=($A)
# Print only the last element via bash array right-hand-side indexing syntax
echo "${A_array[-1]}"  # last element only

For all answers, start with:

Convert A to an array, with elements being separated by the default IFS (Internal Field Separator) char, which is space:

# Capture space-separated words as separate elements in array A_array
A_array=($A)
# Print only the last element via bash array right-hand-side indexing syntax
echo "${A_array[-1]}"  # last element only

(Run each of these commands in your terminal to test this live.)

For all answers below, start by typing this in your terminal:

Convert A to an array, with elements being separated by the default IFS (Internal Field Separator) char, which is space:

  1. Option 1 (will "break in mysterious ways", as @tripleee put it in a comment here, if the string stored in the A variable contains certain special shell characters, so Option 2 below is recommended instead!):
    # Capture space-separated words as separate elements in array A_array
    A_array=($A)
    
  2. Option 2 [RECOMMENDED!]. Use the read command, as I explain in my answer here, and as is recommended by the bash shellcheck static code analyzer tool for shell scripts, here.
    # Capture space-separated words as separate elements in array A_array, using
    # a "herestring". 
    # See my answer here: https://stackoverflow.com/a/71575442/4561887
    read -r -d '' -a A_array <<< "$A"
    

Then, print only the last elment in the array:

# Print only the last element via bash array right-hand-side indexing syntax
echo "${A_array[-1]}"  # last element only
Fix broken quoting
Source Link
tripleee
  • 185.2k
  • 36
  • 295
  • 342

More ways to do this:

For all answers, start with:

A="Some variable has value abc.123"

The array example (#3 below) is a really useful pattern, and depending on what you are trying to do, sometimes the best.

1. with awk, as the main answer shows

echo $A"$A" | awk '{print $NF}'

2. with grep:

echo "$A" | grep -o '[^ ]*$'
  1. the -o says to only retain the matching portion of the string
  2. the [^ ] part says "don't match spaces"; ie: "not the space char"
  3. the * means: "match 0 or more instances of the preceding match pattern (which is [^ ]), and the $ means "match the end of the line." So, this matches the last word after the last space through to the end of the line; ie: abc.123 in this case.

3. via regular bash "indexed" arrays and array indexing

Convert A to an array, with elements being separated by the default IFS (Internal Field Separator) char, which is space:

# Capture space-separated words as separate elements in array A_array
A_array=($A)
# Print only the last element via bash array right-hand-side indexing syntax
echo "${A_array[-1]}"  # last element only

Output:

abc.123

Going further:

What makes this pattern so useful too is that it allows you to easily do the opposite too!: obtain all words except the last one, like this:

array_len="${#A_array[@]}"
array_len_minus_one=$((array_len - 1))
echo "${A_array[@]:0:$array_len_minus_one}"

Output:

Some variable has value

For more on the ${array[@]:start:length} array slicing syntax above, see my answer here: Unix & Linux: Bash: slice of positional parameters, and for more info. on the bash "Arithmetic Expansion" syntax, see here:

  1. https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Arithmetic-Expansion
  2. https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Arithmetic

More ways to do this:

For all answers, start with:

A="Some variable has value abc.123"

The array example (#3 below) is a really useful pattern, and depending on what you are trying to do, sometimes the best.

1. with awk, as the main answer shows

echo $A | awk '{print $NF}'

2. with grep:

echo "$A" | grep -o '[^ ]*$'
  1. the -o says to only retain the matching portion of the string
  2. the [^ ] part says "don't match spaces"; ie: "not the space char"
  3. the * means: "match 0 or more instances of the preceding match pattern (which is [^ ]), and the $ means "match the end of the line." So, this matches the last word after the last space through to the end of the line; ie: abc.123 in this case.

3. via regular bash "indexed" arrays and array indexing

Convert A to an array, with elements being separated by the default IFS (Internal Field Separator) char, which is space:

# Capture space-separated words as separate elements in array A_array
A_array=($A)
# Print only the last element via bash array right-hand-side indexing syntax
echo "${A_array[-1]}"  # last element only

Output:

abc.123

Going further:

What makes this pattern so useful too is that it allows you to easily do the opposite too!: obtain all words except the last one, like this:

array_len="${#A_array[@]}"
array_len_minus_one=$((array_len - 1))
echo "${A_array[@]:0:$array_len_minus_one}"

Output:

Some variable has value

For more on the ${array[@]:start:length} array slicing syntax above, see my answer here: Unix & Linux: Bash: slice of positional parameters, and for more info. on the bash "Arithmetic Expansion" syntax, see here:

  1. https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Arithmetic-Expansion
  2. https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Arithmetic

More ways to do this:

For all answers, start with:

A="Some variable has value abc.123"

The array example (#3 below) is a really useful pattern, and depending on what you are trying to do, sometimes the best.

1. with awk, as the main answer shows

echo "$A" | awk '{print $NF}'

2. with grep:

echo "$A" | grep -o '[^ ]*$'
  1. the -o says to only retain the matching portion of the string
  2. the [^ ] part says "don't match spaces"; ie: "not the space char"
  3. the * means: "match 0 or more instances of the preceding match pattern (which is [^ ]), and the $ means "match the end of the line." So, this matches the last word after the last space through to the end of the line; ie: abc.123 in this case.

3. via regular bash "indexed" arrays and array indexing

Convert A to an array, with elements being separated by the default IFS (Internal Field Separator) char, which is space:

# Capture space-separated words as separate elements in array A_array
A_array=($A)
# Print only the last element via bash array right-hand-side indexing syntax
echo "${A_array[-1]}"  # last element only

Output:

abc.123

Going further:

What makes this pattern so useful too is that it allows you to easily do the opposite too!: obtain all words except the last one, like this:

array_len="${#A_array[@]}"
array_len_minus_one=$((array_len - 1))
echo "${A_array[@]:0:$array_len_minus_one}"

Output:

Some variable has value

For more on the ${array[@]:start:length} array slicing syntax above, see my answer here: Unix & Linux: Bash: slice of positional parameters, and for more info. on the bash "Arithmetic Expansion" syntax, see here:

  1. https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Arithmetic-Expansion
  2. https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Arithmetic
added 58 characters in body
Source Link
Gabriel Staples
  • 47.5k
  • 22
  • 251
  • 331
Loading
added 48 characters in body
Source Link
Gabriel Staples
  • 47.5k
  • 22
  • 251
  • 331
Loading
added 692 characters in body
Source Link
Gabriel Staples
  • 47.5k
  • 22
  • 251
  • 331
Loading
added 692 characters in body
Source Link
Gabriel Staples
  • 47.5k
  • 22
  • 251
  • 331
Loading
Source Link
Gabriel Staples
  • 47.5k
  • 22
  • 251
  • 331
Loading