9

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?

2
  • There is something wrong in your script i guess,you can start by using proper syntax with if. if <COMMANDS>; then <COMMANDS>; fi
    – karthick87
    May 2, 2011 at 2:45
  • 2
    I don't know if it's just me... but try Pitfall 6; I hear it gets a few people ;) mywiki.wooledge.org/BashPitfalls May 2, 2011 at 3:24

2 Answers 2

13

The main error in your script is that the [ command, equivalent to test 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 be

if mkdir "$appdir" && cd "$appdir"; then
  echo "Success"
else
  echo "Failed to create and switch directory"
fi

This is explained in Bash Pitfalls: 9. if [grep foo myfile.

I suggest you go through GrayCat Bash Guide to understand bash.

2
  • That guide is really very good! Thank you, otherwise I would be still deciphering man pages :)
    – Zabba
    May 2, 2011 at 16:34
  • Another way is to check if the dir exists if [ -d "$APPDIR" ]; then cd $APPDIR fi
    – s1mmel
    Jun 21, 2018 at 11:50
-3

A prototype could be:

  • Create a file in your desktop: touch newDirectory.sh
  • Make file executable: chmod +x newDirectory.sh
  • To call the script from a terminal in the desktop : ./newDirectory.sh anyName

/

#!/bin/bash
function abc() {
  appname=${1}
  appdir="$HOME/Desktop/$appname"
  if (( mkdir "${appdir}" )) ; then     
    cd "${appdir}"  
    echo "Success" 
  else   
    echo "Failed to create and switch directory" 
  fi
}
abc ${1}

Little recommendation: if you are new, do not mess with .bashrc :)

9
  • 3
    Sorry, no. There are several problems with that answer. 1) missing a {. 2) it never runs mkdir. 3) you didn't test cd's exit status. 4) Don't put extensions on scripts. Especially not .sh when it's not even an sh-script. 5) Putting it in a script defeats the purpose of the function. mywiki.wooledge.org/BashGuide
    – geirha
    May 2, 2011 at 7:38
  • @geirha: my fault, the { was there before my editing.
    – enzotib
    May 2, 2011 at 7:44
  • Answer: (( mkdir "${appdir}" )) is equal 'mkdir "${appdir}"' (look at the back ticks)
    – studentz
    May 2, 2011 at 14:29
  • debug the script bash -x newDirectory.sh and you will see that the script will cd to the directory. If you want to run another process after that, It is a completely different horse.
    – studentz
    May 2, 2011 at 14:41
  • The script run with bash dash ash. You can change the extension whatever you want.
    – studentz
    May 2, 2011 at 14:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .