Skip to main content
added 156 characters in body
Source Link
John1024
  • 75.2k
  • 11
  • 170
  • 164

You need a function, not an alias. Try:

newfolder() { mkdir -p "$1" && cd "$1"; }

Example:

$ pwd
/tmp
$ newfolder dir1/dir2
$ pwd
/tmp/dir1/dir2

Notes:

  1. The -p option to mkdir tells it to create missing parent directories if needed. In the above example, dir1 did not exist but mkdir -p dir1/dir2 created both dir1 and dir2.

  2. Because we use the shell operator &&, the cd command will only be performed if the mkdir command succeeded.

  3. Aliases are useful in very simple cases where a fixed string can be substituted for a word. Aliases do not process arguments.

  4. Because we are using a shell function in place of an alias, we can reference the arguments, such as, in this case, $1, as neededaccording to our needs.

You need a function, not an alias. Try:

newfolder() { mkdir -p "$1" && cd "$1"; }

Example:

$ pwd
/tmp
$ newfolder dir1/dir2
$ pwd
/tmp/dir1/dir2

Notes:

  1. The -p option to mkdir tells it to create missing parent directories if needed. In the above example, dir1 did not exist but mkdir -p dir1/dir2 created both dir1 and dir2.

  2. Because we use the shell operator &&, the cd command will only be performed if the mkdir command succeeded.

  3. Because we are using a shell function in place of an alias, we can reference the arguments, in this case $1, as needed.

You need a function, not an alias. Try:

newfolder() { mkdir -p "$1" && cd "$1"; }

Example:

$ pwd
/tmp
$ newfolder dir1/dir2
$ pwd
/tmp/dir1/dir2

Notes:

  1. The -p option to mkdir tells it to create missing parent directories if needed. In the above example, dir1 did not exist but mkdir -p dir1/dir2 created both dir1 and dir2.

  2. Because we use the shell operator &&, the cd command will only be performed if the mkdir command succeeded.

  3. Aliases are useful in very simple cases where a fixed string can be substituted for a word. Aliases do not process arguments.

  4. Because we are using a shell function in place of an alias, we can reference the arguments, such as, in this case, $1, according to our needs.

Source Link
John1024
  • 75.2k
  • 11
  • 170
  • 164

You need a function, not an alias. Try:

newfolder() { mkdir -p "$1" && cd "$1"; }

Example:

$ pwd
/tmp
$ newfolder dir1/dir2
$ pwd
/tmp/dir1/dir2

Notes:

  1. The -p option to mkdir tells it to create missing parent directories if needed. In the above example, dir1 did not exist but mkdir -p dir1/dir2 created both dir1 and dir2.

  2. Because we use the shell operator &&, the cd command will only be performed if the mkdir command succeeded.

  3. Because we are using a shell function in place of an alias, we can reference the arguments, in this case $1, as needed.