Narendra Kumar's blog

Bash scripting

Redirect output of a loop to a file

Wrap the loop in parenthisis and then use redirection operator.

(for i in {1..10}; do \
    echo "line number $i"; \
done) > output.txt 2>&1

Feed output of a command into a while loop

I want to open a chrome tab with every url with https scheme in file urls.txt given below.

I can cat this file, filter on https and then use while to open a Google Chrome tab for each such url as shown below:

cat urls.txt | grep "https://" | (while read url; do open -a "Google Chrome" "$url"; done)

Repeat something using for loop

Print parrot names.

numTabs=30; \
beingAt=6899; \
N=$((numTabs-1)); \
for((j=0;j<=N;j++)); do \
   parrotNumber=$(($beginAt - $j)); \
   parrotName="parrot${parrotNumber}"; \
   echo "$parrotName";

Access array by index

Print alternative elements.

array=(apple banana mango rotten republic); \
arrayLength=${#array[@]}; \
for((i=0;i<$arrayLength;i++)); do \
   remainder=$(($i%2)); \
   if [ "$remainder" = "0" ]; then \
     echo "${array[$i]}"; \
   fi \

Print every third element

array=(apple banana mango rotten republic); \
for((i=0;i<${#array[@]};i=i+3)); do \
     echo "${array[$i]}"; \