I have created a simple script to move all the files with .sh suffix in the current working directory to a designated directoryIf there was no such file, the script should output only an error message that I have entered, not the system error message: mv: cannot stat *.sh: No such file or directoryI tried adding ./shm 2> /dev/null after the mv command put that caused the script to run with no stop!How can I mute the systems error message?#!/bin/bashecho Moving all script files to script directory...mv *.sh $HOME/linux/scripts#./shm 2> /dev/nullif [ $? -ne 0 ] then        echo No files with .sh suffixfi
Mute system error message in a script file and display mine
bash;error handling
The below script should be helpful, it will help you in displaying your custom error messages.#!/bin/bashfiles=$(ls *.sh 2> /dev/null)if [ -z $files ]then    echo No script files found. Exiting..    exitfiecho Moving all script files to script directory...for file in $filesdo    if ! mv $file $HOME/linux/scripts    then        echo Unable to move file $file    fidone When the script gets executed, the list of *.sh files in the current working directory of the scripts gets assigned to files variable.Next we do a sanity check to see if $files variable is empty. If its empty then it means that there are no *.sh files in the directory.If the files exist, the for loop iterates over the $files variable and moves every file. If the mv command fails and error will be printed. For more details about for loop refer this document.