I'm trying to create a directory and cd
into it:
In ~/.bashrc:
function abc() {
appname=$1
appdir="$HOME/code/$appname"
if [ mkdir $appdir -a cd $appdir ]; then
echo Success
else
echo Failed to create and switch directory
fi
}
When I reload bashrc (. ~/.bashrc
) I get the error:
bash: [: too many arguments
Failed to create and switch directory
How do I fix this? And what does [:
in the error mean?
Ps. Could someone direct me to a "non-cryptic" bash scripting tutorial?
Best Answer
The main error in your script is that the
[
command, equivalent totest
command, is used to test conditions, like string comparison, existence of files, and so on.To test the exit status of processes you have to use
if
without[
, so your script could beThis is explained in Bash Pitfalls: 9. if [grep foo myfile.
I suggest you go through GrayCat Bash Guide to understand bash.