Linux shell programming : string
Here is a simple script to calculate the sum of a number from 1 to 10:
#!/bin/sh s="12345" len=${#s} echo "len=$len"
Output:
5
The following extracts $length characters of substring from $string at $position:
${string:position:length}
This extracts "substring" from $string at $position:
${string:position}
Example:
#!/bin/bash s="0123456789" len=${#s} echo "s=$s" echo "s:3 => ${s:3}" echo "s:3:len-3 => ${s:3:len-3}" echo "s:3:4 => ${s:3:4}"
Output:
s=0123456789 s:3 => 3456789 s:3:len-3 => 3456789 s:3:4 => 3456
Indexing from the right end of the string?
#!/bin/bash s="0123456789" echo "s=$s" echo "s:-3 => ${s:-3 }" echo "s: -3 => ${s: -3 }" echo "s:(-3) => ${s:(-3)}"
Output:
s=0123456789 s:-3 => 0123456789 s: -3 => 789 s:(-3) => 789
Here we need either a space, ' ' or parentheses, '()' to "escape" the position parameter. Otherwise, it defaults to full string, as in ${position_parameter:-default}.
To reverse a string, we can do with a command rev
:
$ echo abcde | rev
To do the same thing in shell script, we need to know how to extract a single character from a string:
#!/bin/bash s="abcde" len=${#s} echo "1st string at index = 0, ${s:0:1}" echo "last string at index = 0, ${s:len-1:1}"
Output:
1st string at index = 0, a last string at index = 0, e
Note that the substring syntax looks like this:
## syntax ## ${parameter:offset:length}
Now, let's run a loop - extract a character from the end:
#!/bin/bash s="abcde" len=${#s} for ((i=len-1;i>=0;i--)) do echo "index=$i, ${s:$i:1}" done
Output:
index=4, e index=3, d index=2, c index=1, b index=0, a
It's time to reverse the string, constructing it one by one from the end:
#!/bin/bash s="abcde" len=${#s} for ((i=len-1;i>=0;i--)) do rev="$rev${s:$i:1}" echo "index=$i, rev=$rev" done
Output:
index=4, rev=e index=3, rev=ed index=2, rev=edc index=1, rev=edcb index=0, rev=edcba
How to merge strings?:
#!/bin/bash s1="Hello" s2="Strings" echo "$s1 $s2" arr=($s1 $s2) for item in ${arr[@]} do new_s+="$item " done echo $new_s
Output:
Hello Strings Hello Strings
Linux - system, cmds & shell
- Linux Tips - links, vmstats, rsync
- Linux Tips 2 - ctrl a, curl r, tail -f, umask
- Linux - bash I
- Linux - bash II
- Linux - Uncompressing 7z file
- Linux - sed I (substitution: sed 's///', sed -i)
- Linux - sed II (file spacing, numbering, text conversion and substitution)
- Linux - sed III (selective printing of certain lines, selective definition of certain lines)
- Linux - 7 File types : Regular, Directory, Block file, Character device file, Pipe file, Symbolic link file, and Socket file
- Linux shell programming - introduction
- Linux shell programming - variables and functions (readonly, unset, and functions)
- Linux shell programming - special shell variables
- Linux shell programming : arrays - three different ways of declaring arrays & looping with $*/$@
- Linux shell programming : operations on array
- Linux shell programming : variables & commands substitution
- Linux shell programming : metacharacters & quotes
- Linux shell programming : input/output redirection & here document
- Linux shell programming : loop control - for, while, break, and break n
- Linux shell programming : string
- Linux shell programming : for-loop
- Linux shell programming : if/elif/else/fi
- Linux shell programming : Test
- Managing User Account - useradd, usermod, and userdel
- Linux Secure Shell (SSH) I : key generation, private key and public key
- Linux Secure Shell (SSH) II : ssh-agent & scp
- Linux Secure Shell (SSH) III : SSH Tunnel as Proxy - Dynamic Port Forwarding (SOCKS Proxy)
- Linux Secure Shell (SSH) IV : Local port forwarding (outgoing ssh tunnel)
- Linux Secure Shell (SSH) V : Reverse SSH Tunnel (remote port forwarding / incoming ssh tunnel) /)
- Linux Processes and Signals
- Linux Drivers 1
- tcpdump
- Linux Debugging using gdb
- Embedded Systems Programming I - Introduction
- Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu/Fedora
- LXC (Linux Container) Install and Run
- Linux IPTables
- Hadoop - 1. Setting up on Ubuntu for Single-Node Cluster
- Hadoop - 2. Runing on Ubuntu for Single-Node Cluster
- ownCloud 7 install
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox I
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox II
- Windows 8 guest on Mac OSX host using VirtualBox I
- Ubuntu Package Management System (apt-get vs dpkg)
- RPM Packaging
- How to Make a Self-Signed SSL Certificate
- Linux Q & A
- DevOps / Sys Admin questions
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization