I have a string:


I need to save in a variable A value two and in variable B value fourfrom the above string

I am using ksh.

8 Answers 8


Use cut with _ as the field delimiter and get desired fields:

A="$(cut -d'_' -f2 <<<'one_two_three_four_five')"
B="$(cut -d'_' -f4 <<<'one_two_three_four_five')"

You can also use echo and pipe instead of Here string:

A="$(echo 'one_two_three_four_five' | cut -d'_' -f2)"
B="$(echo 'one_two_three_four_five' | cut -d'_' -f4)"


$ s='one_two_three_four_five'

$ A="$(cut -d'_' -f2 <<<"$s")"
$ echo "$A"

$ B="$(cut -d'_' -f4 <<<"$s")"
$ echo "$B"

Beware that if $s contains newline characters, that will return a multiline string that contains the 2nd/4th field in each line of $s, not the 2nd/4th field in $s.

  • Is there any alternative? I am using ksh (not bsh) and it returns ksh: syntax error: `<' unexpected
    – Alex
    Sep 25, 2016 at 23:14
  • @Alex Check my edits.
    – heemayl
    Sep 25, 2016 at 23:24
  • Nice answer, I have a little question: what happen if your variable "$s" is a path folder. When I try to cut a path folder I do like following: ` $FILE=my_user/my_folder/[file]*` $ echo $FILE my_user/my_folder/file.csv $ A="$(cut -d'/' -f2 <<<"$FILE")" $ echo $A [file]* Do you know what's happening here? Dec 5, 2018 at 11:55
  • 2
    And if you just want the last field, using only shell builtins - without needing to specify its position, or when you don't know the number of fields: echo "${s##*_}"
    – Amit Naidu
    May 28, 2019 at 21:45
  • Note that since it involves forking at least one process, executing a separate command, passing the data between processes several times via pipes and/or temporary files, it's a lot less efficient than solutions that use the built-in splitting operators of your shell. Jun 10, 2020 at 6:53

Wanted to see an awk answer, so here's one:

A=$(awk -F_ '{print $2}' <<< 'one_two_three_four_five')
B=$(awk -F_ '{print $4}' <<< 'one_two_three_four_five')  

Try it online!

  • 4
    And if you want the last piece - without needing to specify its position or when you don't know the number of fields: awk -F_ '{print $NF}' <<< 'one_two_3_4_five'
    – Amit Naidu
    May 28, 2019 at 21:38
  • 2
    this is exactly what i was looking for !!! thanks a ton and this is the most simplest and straightforward way. Nov 9, 2020 at 10:58

Using only POSIX sh constructs, you can use parameter substitution constructs to parse one delimiter at a time. Note that this code assumes that there is the requisite number of fields, otherwise the last field is repeated.

first="${remainder%%_*}"; remainder="${remainder#*_}"
second="${remainder%%_*}"; remainder="${remainder#*_}"
third="${remainder%%_*}"; remainder="${remainder#*_}"
fourth="${remainder%%_*}"; remainder="${remainder#*_}"

Alternatively, you can use an unquoted parameter substitution with wildcard expansion disabled and IFS set to the delimiter character (this only works if the delimiter is a single non-whitespace character or if any whitespace sequence is a delimiter).

set -f; IFS='_'
set -- $string
second=$2; fourth=$4
set +f; unset IFS

This clobbers the positional parameters. If you do this in a function, only the function's positional parameters are affected.

Yet another approach for strings that don't contain newline characters is to use the read builtin.

IFS=_ read -r first second third fourth trail <<'EOF'
  • The use of unset IFS doesn't return IFS to default. If after that someone does OldIFS="$IFS" will have a null value inside OldIFS. Also, it is assuming that the previous value of IFS is the default, which is very possible (and useful) to not be. The only correct solution is to store in old="$IFS" and later restore with IFS="$old". Or ... use a sub-shell (...). Or, better yet, read my answer.
    – done
    Sep 26, 2016 at 16:09
  • @sorontar unset IFS doesn't restore IFS to the default value, but it returns field splitting to the default effect. Yes, it's a limitation, but usually an acceptable one in practice. The problem with a subshell is that we need to get data out of it. I do show a solution that doesn't change the state at the end, with read. (It works in POSIX shells, but IIRC not in the Bourne shell because it would run the read in a subshell due to the here-document.) Using <<< as in you answer is a variant that works only in ksh/bash/zsh. Sep 26, 2016 at 16:23
  • I don't see a problem even with att or heirloom shell about a subshell. All shells tested (including the old bourne) provide the correct value in the main shell.
    – done
    Sep 26, 2016 at 19:16
  • What happen if my path is something like user/my_folder/[this_is_my_file]*? What I obtain when I follow these steps is [this_is_my_file]* Dec 5, 2018 at 12:20
  • @HenryNavarro This output doesn't correspond to any of the code snippets in my answer. None of them do anything special on /. Dec 5, 2018 at 17:11

Here string

The simplest way (for shells with <<<) is:

 IFS='_' read -r a second a fourth a <<<"$string"

Using a temporal variable $a instead of $_ because one shell complains.

In a full script:

 IFS='_' read -r a second a fourth a <<<"$string"
 echo "$second $fourth"

No IFS changing, not issues with set -f (Pathname expansion) No changes to the positional parameters ("$@").

For a solution portable to all shells (yes, all POSIX included) without changing IFS or set -f, use the (a bit more complex) heredoc equivalent:


IFS='_' read -r a second a fourth a <<-_EOF_

echo "$second $fourth"

Understand that this solutions (both the here-doc and the use of <<< will remove all trailing newlines.
And that this is designed to a "one liner" variable content.
Solutions for multi-liners are possible but need more complex constructs.

Bash 4.4+
A very simple solution is possible in bash version 4.4

readarray -d _ -t arr <<<"$string"

echo "array ${arr[1]} ${arr[3]}"   # array numbers are zero based.

There is no equivalent for POSIX shells, as many POSIX shells do not have arrays.

For shells that have arrays may be as simple as:
(tested working in attsh, lksh, mksh, ksh, and bash (not zsh))

set -f; IFS=_; arr=($string)

But with a lot of additional plumbing to keep and reset variables and options:

string='one_* *_three_four_five'

case $- in
    *f*) noglobset=true; ;;
    *) noglobset=false;;


set -f; IFS=_; arr=($string)

if $noglobset; then set -f; else set +f; fi


echo "two=${arr[1]} four=${arr[3]}"

In zsh, arrays start in 1, and doesn't split string by default.
So some changes need to be done to get this working in zsh:

set -F; IFS=_; arr=( $(echo $string) )
echo "two=${arr[2]} four=${arr[4]}"

With zsh you could split the string (on _) into an array:


and then access each/any element via array index:

print -r -- ${all_elements[4]}

Keep in mind that in zsh (like most other shells, but unlike ksh/bash) array indices start at 1.

Or directly in one expansion:

print -r -- "${${(@s:_:)string}[4]}"
  • @sorontar - why do you think I need set -f ? I'm not using read/IFS. Try my solutions with a string like *_*_* or whatever... Sep 26, 2016 at 19:16
  • Not for zsh, but the user asked for a ksh solution, so, he may try to use it in that shell. A warning will help him avoid the problem.
    – done
    Sep 26, 2016 at 19:19
  • @Isaac, if you want to disable globbing in zsh, that would rather be set -o noglob. The -f option in zsh's default emulation mode, is the same as in csh, not sh, it's about starting zsh without reading conf files. set -f to disable globbing would only work with sh/ksh emulation (that would be the emulations where you would need to disable globbing indeed, but you'd only use those modes to interpret sh/ksh code, so it doesn't really apply here, and you'd need to change the code further anyway to work around the other misfeatures of sh/ksh) Jun 10, 2020 at 6:24
  • Exactly @StéphaneChazelas. If the user wants to use this solution in ksh (Please read I am using ksh) then he might need set -f. Not for zsh, I am not saying to use set -f in zsh.
    – done
    Jun 12, 2020 at 18:09

Is a python solution allowed?

# python -c "import sys; print sys.argv[1].split('_')[1]" one_two_three_four_five

# python -c "import sys; print sys.argv[1].split('_')[3]" one_two_three_four_five
  • 1
    No. bad bad answet
    – Raj Kumar
    May 9, 2019 at 7:51

With due respect to everyone who have posted excellent answers, I wonder if we are over-engineering this problem. Three simple lines to just answer the question asked without generalizing:

str="one_two_three_four_five" <-- create a string

A=$(echo $str | awk -F_ '{print $2}') <-- tell awk to use _ as the delimiter and assign the second field to A

B=$(echo $str | awk -F_ '{print $4}') <-- tell awk to use _ as the delimiter and assign the fourth field to B

You can then use the variables as usual. Here is an example:

$ echo "The value of A is: $A; the value of B is: $B"
The value of A is: two; the value of B is: four
  • That answer has already been given (several times). Try with str='*' or str='-n _-o _-p ' or str=$'a_b\n_c_d_e'. Jun 10, 2020 at 6:35

Another awk example; simpler to understand.

A=$(echo one_two_three_four_five | awk -F_ '{print $1}')
B=$(echo one_two_three_four_five | awk -F_ '{print $2}')  
C=$(echo one_two_three_four_five | awk -F_ '{print $3}')  
... and so on...  

Can be used with variables also.



Then the following works:

A=$(printf '%s\n' "${this_str}" | awk -F_ '{print $1}')
B=$(printf '%s\n' "${this_str}" | awk -F_ '{print $2}')
C=$(printf '%s\n' "${this_str}" | awk -F_ '{print $3}')
... and so on...  

That assumes ${this_str} doesn't contain newline characters, or it would return the first _ in each line of the contents of the variable instead of the first field in the contents of the variable.

  • Yea, this is the most readable solution, although keep in mind this will run pretty slow given there are many subshell invocations used.
    – Yuri
    May 14, 2021 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.