Sunday, 28 June 2020

How do I make curl ignore the proxy?

curl --noproxy "*" http://www.stackoverflow.com

vi ~/.curlrc
noproxy=*
proxy=yourproxy.com:8080

Replace one substring for another string in shell script

#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
echo "${firstString/Suzi/$secondString}"    
# prints 'I love Sara and Marry'
EXAMPLE 2:
URL="http://hostname/project/branches/Old_Branch/package"
SRC="Old_Branch"
REP="New_Branch"

echo "${URL/$SRC/$REP}"

Loop through an array of strings in Bash?

## declare an array variable
declare -a arr=("element1" "element2" "element3")

## now loop through the above array
for i in "${arr[@]}"
do
   echo "$i"
   # or do whatever with individual element of the array
done

# You can access them using echo "${arr[0]}", "${arr[1]}" also

In Bash, how can I check if a string begins with some value?

This snippet on the Advanced Bash Scripting Guide says:

# The == comparison operator behaves differently within a double-brackets
# test than within single brackets.

[[ $a == z* ]]   # True if $a starts with a "z" (wildcard matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).

string='My long string'
if [[ $string == *"My long"* ]]; then
  echo "It's there!"
fi

Print array elements on separate lines in Bash?

Try doing this :

$ printf '%s\n' "${my_array[@]}"