You can translate the content of this page by selecting a language in the select box.
How to reverse a string on Linux and Windows
On Linux:
- Using the
rev
command: Therev
command is a utility that reverses the lines of a file or the characters in a string. To reverse a string, you can use theecho
command to pass the string torev
:
echo "string" | rev
- Using the
sed
command: Thesed
command is a powerful utility that can perform various text transformations. To reverse a string, you can use thesed
command with the-r
option and the's/.*(.)/\1/g'
expression:
echo "string" | sed -r 's/.*(.)/\1/g'
- Using the
awk
command: Theawk
command is a programming language that is used for text processing. To reverse a string, you can use theawk
command with the{print}
action:
echo "string" | awk '{print $1}'
On Windows:
- Using the
powershell
command: Thepowershell
command is a shell that provides a command-line interface for Windows. To reverse a string, you can use thepowershell
command with the-C
option and the'[System.Text.Encoding]::Unicode.GetString([System.Text.Encoding]::Unicode.GetBytes("string"))'
expression:
powershell -C "[System.Text.Encoding]::Unicode.GetString([System.Text.Encoding]::Unicode.GetBytes("string"))"
- Using the
cmd
command: Thecmd
command is the command-line interpreter for Windows. To reverse a string, you can use thecmd
command with thefor
loop:
cmd /c "for /L %i in (1,1,%len%) do @echo !string:~%len%-%i,1!"
These are some ways to reverse a string on Linux and Windows. There are other ways to achieve this, using different utilities or programming languages.
Via shell script on Linux
sh-3.2# vi reverse.sh
#### Start Script #####
#!/bin/bash
input_string=”$1″
reverse_string=””
input_string_length=${#input_string}
for (( i=$input_string_length-1; i>=0; i– ))
do
reverse_string=”$reverse_string${input_string:$i:1}”
done
echo “$reverse_string”
##### End Script #####
Let’s run it:
sh-3.2# chmod 775 reverse.sh
sh-3.2# ./reverse.sh Etienne
enneitE
Via powershell script on Windows
#Let’s use the script reverse.ps1 below.
######
$string=”Etienne”
$string_array=$string -split “”
[array]::Reverse($string_array)
$string_array -join ”
#####Output#####
PS C:\Users\etienne_noumen\Documents\Etienne\Scripting> .\reverse.ps1
E t i e n n e
If you are looking for an all-in-one solution to help you prepare for the AWS Cloud Practitioner Certification Exam, look no further than this AWS Cloud Practitioner CCP CLFC01 book below.

enneitE
Via powershell script on Windows in one line
([regex]::Matches($String,’.’,’RightToLeft’) | ForEach {$_.value}) -join ”
Via batch script on Windows
::Note: ReverseStr also calls StrLen
::and string length is not greater than 80 chars
:: but can be changed.
@echo off
SetLocal EnableDelayedExpansion
cls
set Str=Etienne
call :StrLen %Str%
echo Length=%Len%
call :ReverseStr %Str%
echo String=%Str%
echo Reverse Str=%Reverse%
exit /b
::—————-
:: Calc Var Length
::—————-
:: %*=Str to Check
:: Returns %Len%
:: —————
:StrLen %*
set Data=%*
for /L %%a in (0,1,80) do (
set Char=!Data:~%%a,1!
if not “!Char!”==”” (
set /a Len=%%a+1
) else (exit /b)
)
exit /b
::—————
:: Reverse String
::—————
:: %* Str to Reverse
:: Returns %Reverse%
::——————
:ReverseStr %*
set Data=%*
call :StrLen %Data%
for /L %%a in (!Len!,-1,0) do (
set Char=!Data:~%%a,1!
set Reverse=!Reverse!!Char!
)
exit /b
Via perl script on Windows or Linux
Via python script on Windows or Linux
def reverse_string(a_string)
return a_string[::-1]
reverse_string(“etienne”) returns “enneite”
Source: