Bash Scripting

Foreach in Cat File

# my_script.sh
cat deps.txt | while read line
do
   echo "$line"
done

Output to cmd and file at the same time

# the command output will be displaied to the terminal
# and will be writen in the myfile.txt
my_comand | tee myfile.txt

Check if command exists

command_exists () {
    if [[ $(command -v $1) ]]; then
        return 0
    fi

    return 1
}

if command_exists "MyCommandHere"; then
    echo "MyCommandHere exists"
fi


# real world sample

if command_exists "exa"; then
    alias ls="exa"
fi

Last updated

Was this helpful?