What is the best way to turn out digits in number?


$ echo 123 | hook
$ echo 12358 | hook
:~> echo 123456 | rev

It should work. The usage is pretty simple. from man:

rev - reverse lines of a file or files

rev [file ...]

The rev utility copies the specified files to the standard output, reversing the order of characters in every line. If no files are specified, the standard input is read.

If not available you should also be able to use awk:

:~> echo 123456 | awk '{ for(i=length;i!=0;i--)x=x substr($0,i,1);}END{print x}'

Source the unix school

If you eventually need to completly invert a file you could add tac to rev command line like this:

tac myfile | rev 
  • Which definition of character does it use? Byte, code-unit, code-point, or does it even a full combining character sequence? – CodesInChaos Mar 24 '14 at 18:10
  • What? I dont understand your question – Kiwy Mar 24 '14 at 18:12
  • I'm interested in how well rev understands text (and thus charsets and unicode), or if it simply operates on bytes. Consider an input which contains characters outside ASCII. For example with UTF-8, one code-point often maps to two bytes. If you reverse the bytes, that will produce a broken output. Same for legacy multi-byte charsets like they're common in Asia. Similarly if you have a letter followed by a combining diacritic, simply reversing the code-points means that it will combine with the wrong base letter. – CodesInChaos Mar 24 '14 at 18:20
  • 2
    @CodesInChaos rev operates on bytes, but uses the current locale to interpret them. Compare LANG=utf8 rev file.txt vs LANG=C rev file.txt for some UTF-8 encoded file with non-ASCII characters. With utf8, rev will recognize multibyte UTF-8 sequences and preserve their relative order. – chepner Mar 24 '14 at 19:10
  • 3
    @chepner, GNU rev reverses the order of characters in each line, not bytes, not combining sequences. It will turn Stéphane to enahpétS but Stéphane to enahṕetS (that is the accent will go to the p instead of the e) – Stéphane Chazelas Mar 24 '14 at 20:00

For those not limited to bash, with zsh:


Those are variable expansion flags:

  • s::: split on the empty string, so one element per character
  • Oa: reverse the resulting array (O for reverse order, a for array subscript order).
  • j::: join the elements with the empty string.

Are you looking for rev?

echo 123|rev
#set -xv


while (( "$string_length">=1 )) 
     string_length=$(( $string_length - 1 ))

echo "$reverse_string"

make the file executable

$ chmod +x reverse_string
$ ./reverse_string 123456789    
  • Or somewhat more compactly: for ((i=${#1}-1; i>=0; i--)); do echo -n "${1:i:1}"; done; echo – Digital Trauma Mar 24 '14 at 19:02

rev is the obvious and probably the best answer. But rev is not always installed by default. If you want to do this purely with coreutils you can use this longer command:

echo 123 | fold -w1 | tac | paste -d '' -s -
  • fold -w1 puts each digit on a line
  • tac is a reverse cat, i.e. reverses the order of the lines
  • paste -d '' -s - serially pastes the digit from each line back onto one line
  • 1
    Neither rev nor tac are standard Unix commands, but rev is a lot more portable than tac. tac is GNU only. Other unices usually have tail -r for tac. – Stéphane Chazelas Mar 24 '14 at 19:55
  • Unlike rev, this can also be used on OpenWRT. – bk138 Mar 27 '15 at 0:30
for i in $*
    s="$i $s";
for j in $s
    echo $j
  • 2
    How is that answering the question? – Stéphane Chazelas May 23 '14 at 12:37
  • 1
    It's clearly attempting to, it just gets it kind of wrong. He wrote a script that will output its arguments in reverse order, instead of a script that will reverse its stdin – Michael Mrozek May 23 '14 at 15:13

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.