I'm trying to create a directory and cd into it:

In ~/.bashrc:

function abc() {
  if [ mkdir $appdir -a cd $appdir ]; then
    echo Success
    echo Failed to create and switch directory

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?

  • 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 '11 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 – Alex Stevens May 2 '11 at 3:24

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"
  echo "Failed to create and switch directory"

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

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

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

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


function abc() {
  if (( mkdir "${appdir}" )) ; then     
    cd "${appdir}"  
    echo "Success" 
    echo "Failed to create and switch directory" 
abc ${1}

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

  • 2
    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 '11 at 7:38
  • @geirha: my fault, the { was there before my editing. – enzotib May 2 '11 at 7:44
  • Answer: (( mkdir "${appdir}" )) is equal 'mkdir "${appdir}"' (look at the back ticks) – studentz May 2 '11 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 '11 at 14:41
  • The script run with bash dash ash. You can change the extension whatever you want. – studentz May 2 '11 at 14:43

protected by heemayl Jan 26 '17 at 3:05

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.