How to make `cd dir/filename` take me to dir/?

Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
I would find it very convenient to be able to use cd with a file argument.
cd myDirectory/anyname.anyExtension
would be equivalent tocd myDirectory/
What would be the best alias or function to achieve this behavior ?
EDIT:
Sorry I didn't mention it in the 1st place: I use zsh
shell cd-command
add a comment |
up vote
8
down vote
favorite
I would find it very convenient to be able to use cd with a file argument.
cd myDirectory/anyname.anyExtension
would be equivalent tocd myDirectory/
What would be the best alias or function to achieve this behavior ?
EDIT:
Sorry I didn't mention it in the 1st place: I use zsh
shell cd-command
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I would find it very convenient to be able to use cd with a file argument.
cd myDirectory/anyname.anyExtension
would be equivalent tocd myDirectory/
What would be the best alias or function to achieve this behavior ?
EDIT:
Sorry I didn't mention it in the 1st place: I use zsh
shell cd-command
I would find it very convenient to be able to use cd with a file argument.
cd myDirectory/anyname.anyExtension
would be equivalent tocd myDirectory/
What would be the best alias or function to achieve this behavior ?
EDIT:
Sorry I didn't mention it in the 1st place: I use zsh
shell cd-command
shell cd-command
edited May 22 '13 at 13:26
asked May 17 '13 at 16:39
Sébastien
312310
312310
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
up vote
5
down vote
accepted
In zsh, I often do:
cd /path/to/somefile(:h)
(h for head).
If somefile is a symlink, you can also do:
cd somefile(:A:h)
To get to the directory where the target of the symlink may be found.
The zsh equivalent of Chris' now bash-only solution would be:
cd() [[ -d $argv[-1] ]]
In zsh, you can also redefine what "words" Ctrl-W removes.
In zsh, "words" in the context of the word-based motion/transpose/delete widgets are sequences of alnums plus the characters in the $WORDCHARS variable which by default includes /.
You could remove / from $WORDCHARS so that Ctrl-W only deletes one path component:
WORDCHARS=$WORDCHARS//
Another useful extension is the select-word-style widget which you can use to interactively choose between different word styles.
autoload select-word-style
zle -N select-word-style
bindkey 'ew' select-word-style
Then pressing Alt-W allows you to choose between different word styles.
$ cd /blah/blih<Alt-W>
Word styles (hit return for more detail):
(b)ash (n)ormal (s)hell (w)hitespace (d)efault (q)uit
(B), (N), (S), (W) as above with subword matching
?
Interesting. (I use zsh too).
– Sébastien
May 18 '13 at 9:17
Very interesting edit on the power ofzshand word styles. Thank you also for thezshcommand. It does the job perfectly.
– Sébastien
May 22 '13 at 13:33
add a comment |
up vote
15
down vote
I assume you still want to retain the original functionality if you input a directory, and you are using bash.
cd()
local file="$!#"
if (( "$#" )) && ! [[ -d "$file" ]]; then
builtin cd "$@:1:($#-1)" "$file%/*"
else
builtin cd "$@"
fi
If you are never going to use cd's options (-P, etc), then this will also suffice:
cd() [ -z "$1" ]; then
builtin cd "$@"
else
builtin cd "$1%/*"
fi
4
if you change the function name to cd make sure to addbuiltinin front of thecdcalls
– Ulrich Dangel
May 17 '13 at 17:22
@Chris: You assumed right ;) Thanks for this ready to use function
– Sébastien
May 18 '13 at 9:21
The version that preservescdoptions does not work withzsh. The simpler one works fine withzsh, BUTcdis no longer equivalent ascd ~:/
– Sébastien
May 22 '13 at 13:24
@Sebastien Try now, I think that should fix that.
– Chris Down
May 22 '13 at 14:20
@Chris, indeed adding a check on[ -z "$1" ]was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution forzsh.
– Sébastien
May 23 '13 at 8:18
add a comment |
up vote
7
down vote
You could use dirname to strip the filename from the path, e.g.
mycd() cd "$(dirname "$1")";
See man dirname.
3
Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
– Chris Down
May 17 '13 at 18:08
1
@ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
– Adrian Frühwirth
May 17 '13 at 18:10
1
@AdrianFrühwirth An exercise! Hah!
– user13742
May 17 '13 at 18:24
add a comment |
up vote
3
down vote
If you add this to your .profile, then load it (source ~/.profile or log out and log in again), then mycd [file or directory] will take you to the right directory:
mycd() if [ -d "$1" ]; then cd "$1"; else cd "$( dirname "$1" )"; fi ;
If you name it cd, then strange things will happen.
1
This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
– Chris Down
May 17 '13 at 17:38
@ChrisDown, though that's true of other shells, you don't need the closing semicolon inzsh.
– Stéphane Chazelas
May 18 '13 at 9:42
Also, this breaks if passing args tocd, like-P.
– Chris Down
May 18 '13 at 11:37
add a comment |
up vote
0
down vote
cd2()
arg=() dir= cmd= IFS=" " msg='[-L
Given a directory called$(sudo reboot), this function may reboot the system. Also, the user is usingzsh, notbash.
– Kusalananda
Dec 2 at 9:34
Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
– ecwpz91
Dec 3 at 23:57
No,cd '$(sudo reboot)'would change directory, while your function would try to evaluate the name.
– Kusalananda
Dec 4 at 6:17
Ah, I see and made changes. Let me know if that works? Good catch.
– ecwpz91
Dec 5 at 7:21
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
In zsh, I often do:
cd /path/to/somefile(:h)
(h for head).
If somefile is a symlink, you can also do:
cd somefile(:A:h)
To get to the directory where the target of the symlink may be found.
The zsh equivalent of Chris' now bash-only solution would be:
cd() [[ -d $argv[-1] ]]
In zsh, you can also redefine what "words" Ctrl-W removes.
In zsh, "words" in the context of the word-based motion/transpose/delete widgets are sequences of alnums plus the characters in the $WORDCHARS variable which by default includes /.
You could remove / from $WORDCHARS so that Ctrl-W only deletes one path component:
WORDCHARS=$WORDCHARS//
Another useful extension is the select-word-style widget which you can use to interactively choose between different word styles.
autoload select-word-style
zle -N select-word-style
bindkey 'ew' select-word-style
Then pressing Alt-W allows you to choose between different word styles.
$ cd /blah/blih<Alt-W>
Word styles (hit return for more detail):
(b)ash (n)ormal (s)hell (w)hitespace (d)efault (q)uit
(B), (N), (S), (W) as above with subword matching
?
Interesting. (I use zsh too).
– Sébastien
May 18 '13 at 9:17
Very interesting edit on the power ofzshand word styles. Thank you also for thezshcommand. It does the job perfectly.
– Sébastien
May 22 '13 at 13:33
add a comment |
up vote
5
down vote
accepted
In zsh, I often do:
cd /path/to/somefile(:h)
(h for head).
If somefile is a symlink, you can also do:
cd somefile(:A:h)
To get to the directory where the target of the symlink may be found.
The zsh equivalent of Chris' now bash-only solution would be:
cd() [[ -d $argv[-1] ]]
In zsh, you can also redefine what "words" Ctrl-W removes.
In zsh, "words" in the context of the word-based motion/transpose/delete widgets are sequences of alnums plus the characters in the $WORDCHARS variable which by default includes /.
You could remove / from $WORDCHARS so that Ctrl-W only deletes one path component:
WORDCHARS=$WORDCHARS//
Another useful extension is the select-word-style widget which you can use to interactively choose between different word styles.
autoload select-word-style
zle -N select-word-style
bindkey 'ew' select-word-style
Then pressing Alt-W allows you to choose between different word styles.
$ cd /blah/blih<Alt-W>
Word styles (hit return for more detail):
(b)ash (n)ormal (s)hell (w)hitespace (d)efault (q)uit
(B), (N), (S), (W) as above with subword matching
?
Interesting. (I use zsh too).
– Sébastien
May 18 '13 at 9:17
Very interesting edit on the power ofzshand word styles. Thank you also for thezshcommand. It does the job perfectly.
– Sébastien
May 22 '13 at 13:33
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
In zsh, I often do:
cd /path/to/somefile(:h)
(h for head).
If somefile is a symlink, you can also do:
cd somefile(:A:h)
To get to the directory where the target of the symlink may be found.
The zsh equivalent of Chris' now bash-only solution would be:
cd() [[ -d $argv[-1] ]]
In zsh, you can also redefine what "words" Ctrl-W removes.
In zsh, "words" in the context of the word-based motion/transpose/delete widgets are sequences of alnums plus the characters in the $WORDCHARS variable which by default includes /.
You could remove / from $WORDCHARS so that Ctrl-W only deletes one path component:
WORDCHARS=$WORDCHARS//
Another useful extension is the select-word-style widget which you can use to interactively choose between different word styles.
autoload select-word-style
zle -N select-word-style
bindkey 'ew' select-word-style
Then pressing Alt-W allows you to choose between different word styles.
$ cd /blah/blih<Alt-W>
Word styles (hit return for more detail):
(b)ash (n)ormal (s)hell (w)hitespace (d)efault (q)uit
(B), (N), (S), (W) as above with subword matching
?
In zsh, I often do:
cd /path/to/somefile(:h)
(h for head).
If somefile is a symlink, you can also do:
cd somefile(:A:h)
To get to the directory where the target of the symlink may be found.
The zsh equivalent of Chris' now bash-only solution would be:
cd() [[ -d $argv[-1] ]]
In zsh, you can also redefine what "words" Ctrl-W removes.
In zsh, "words" in the context of the word-based motion/transpose/delete widgets are sequences of alnums plus the characters in the $WORDCHARS variable which by default includes /.
You could remove / from $WORDCHARS so that Ctrl-W only deletes one path component:
WORDCHARS=$WORDCHARS//
Another useful extension is the select-word-style widget which you can use to interactively choose between different word styles.
autoload select-word-style
zle -N select-word-style
bindkey 'ew' select-word-style
Then pressing Alt-W allows you to choose between different word styles.
$ cd /blah/blih<Alt-W>
Word styles (hit return for more detail):
(b)ash (n)ormal (s)hell (w)hitespace (d)efault (q)uit
(B), (N), (S), (W) as above with subword matching
?
edited May 18 '13 at 20:41
answered May 17 '13 at 18:14
Stéphane Chazelas
297k54561907
297k54561907
Interesting. (I use zsh too).
– Sébastien
May 18 '13 at 9:17
Very interesting edit on the power ofzshand word styles. Thank you also for thezshcommand. It does the job perfectly.
– Sébastien
May 22 '13 at 13:33
add a comment |
Interesting. (I use zsh too).
– Sébastien
May 18 '13 at 9:17
Very interesting edit on the power ofzshand word styles. Thank you also for thezshcommand. It does the job perfectly.
– Sébastien
May 22 '13 at 13:33
Interesting. (I use zsh too).
– Sébastien
May 18 '13 at 9:17
Interesting. (I use zsh too).
– Sébastien
May 18 '13 at 9:17
Very interesting edit on the power of
zsh and word styles. Thank you also for the zsh command. It does the job perfectly.– Sébastien
May 22 '13 at 13:33
Very interesting edit on the power of
zsh and word styles. Thank you also for the zsh command. It does the job perfectly.– Sébastien
May 22 '13 at 13:33
add a comment |
up vote
15
down vote
I assume you still want to retain the original functionality if you input a directory, and you are using bash.
cd()
local file="$!#"
if (( "$#" )) && ! [[ -d "$file" ]]; then
builtin cd "$@:1:($#-1)" "$file%/*"
else
builtin cd "$@"
fi
If you are never going to use cd's options (-P, etc), then this will also suffice:
cd() [ -z "$1" ]; then
builtin cd "$@"
else
builtin cd "$1%/*"
fi
4
if you change the function name to cd make sure to addbuiltinin front of thecdcalls
– Ulrich Dangel
May 17 '13 at 17:22
@Chris: You assumed right ;) Thanks for this ready to use function
– Sébastien
May 18 '13 at 9:21
The version that preservescdoptions does not work withzsh. The simpler one works fine withzsh, BUTcdis no longer equivalent ascd ~:/
– Sébastien
May 22 '13 at 13:24
@Sebastien Try now, I think that should fix that.
– Chris Down
May 22 '13 at 14:20
@Chris, indeed adding a check on[ -z "$1" ]was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution forzsh.
– Sébastien
May 23 '13 at 8:18
add a comment |
up vote
15
down vote
I assume you still want to retain the original functionality if you input a directory, and you are using bash.
cd()
local file="$!#"
if (( "$#" )) && ! [[ -d "$file" ]]; then
builtin cd "$@:1:($#-1)" "$file%/*"
else
builtin cd "$@"
fi
If you are never going to use cd's options (-P, etc), then this will also suffice:
cd() [ -z "$1" ]; then
builtin cd "$@"
else
builtin cd "$1%/*"
fi
4
if you change the function name to cd make sure to addbuiltinin front of thecdcalls
– Ulrich Dangel
May 17 '13 at 17:22
@Chris: You assumed right ;) Thanks for this ready to use function
– Sébastien
May 18 '13 at 9:21
The version that preservescdoptions does not work withzsh. The simpler one works fine withzsh, BUTcdis no longer equivalent ascd ~:/
– Sébastien
May 22 '13 at 13:24
@Sebastien Try now, I think that should fix that.
– Chris Down
May 22 '13 at 14:20
@Chris, indeed adding a check on[ -z "$1" ]was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution forzsh.
– Sébastien
May 23 '13 at 8:18
add a comment |
up vote
15
down vote
up vote
15
down vote
I assume you still want to retain the original functionality if you input a directory, and you are using bash.
cd()
local file="$!#"
if (( "$#" )) && ! [[ -d "$file" ]]; then
builtin cd "$@:1:($#-1)" "$file%/*"
else
builtin cd "$@"
fi
If you are never going to use cd's options (-P, etc), then this will also suffice:
cd() [ -z "$1" ]; then
builtin cd "$@"
else
builtin cd "$1%/*"
fi
I assume you still want to retain the original functionality if you input a directory, and you are using bash.
cd()
local file="$!#"
if (( "$#" )) && ! [[ -d "$file" ]]; then
builtin cd "$@:1:($#-1)" "$file%/*"
else
builtin cd "$@"
fi
If you are never going to use cd's options (-P, etc), then this will also suffice:
cd() [ -z "$1" ]; then
builtin cd "$@"
else
builtin cd "$1%/*"
fi
edited May 22 '13 at 14:19
answered May 17 '13 at 17:07
Chris Down
78.5k13187200
78.5k13187200
4
if you change the function name to cd make sure to addbuiltinin front of thecdcalls
– Ulrich Dangel
May 17 '13 at 17:22
@Chris: You assumed right ;) Thanks for this ready to use function
– Sébastien
May 18 '13 at 9:21
The version that preservescdoptions does not work withzsh. The simpler one works fine withzsh, BUTcdis no longer equivalent ascd ~:/
– Sébastien
May 22 '13 at 13:24
@Sebastien Try now, I think that should fix that.
– Chris Down
May 22 '13 at 14:20
@Chris, indeed adding a check on[ -z "$1" ]was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution forzsh.
– Sébastien
May 23 '13 at 8:18
add a comment |
4
if you change the function name to cd make sure to addbuiltinin front of thecdcalls
– Ulrich Dangel
May 17 '13 at 17:22
@Chris: You assumed right ;) Thanks for this ready to use function
– Sébastien
May 18 '13 at 9:21
The version that preservescdoptions does not work withzsh. The simpler one works fine withzsh, BUTcdis no longer equivalent ascd ~:/
– Sébastien
May 22 '13 at 13:24
@Sebastien Try now, I think that should fix that.
– Chris Down
May 22 '13 at 14:20
@Chris, indeed adding a check on[ -z "$1" ]was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution forzsh.
– Sébastien
May 23 '13 at 8:18
4
4
if you change the function name to cd make sure to add
builtin in front of the cd calls– Ulrich Dangel
May 17 '13 at 17:22
if you change the function name to cd make sure to add
builtin in front of the cd calls– Ulrich Dangel
May 17 '13 at 17:22
@Chris: You assumed right ;) Thanks for this ready to use function
– Sébastien
May 18 '13 at 9:21
@Chris: You assumed right ;) Thanks for this ready to use function
– Sébastien
May 18 '13 at 9:21
The version that preserves
cd options does not work with zsh. The simpler one works fine with zsh, BUT cd is no longer equivalent as cd ~ :/– Sébastien
May 22 '13 at 13:24
The version that preserves
cd options does not work with zsh. The simpler one works fine with zsh, BUT cd is no longer equivalent as cd ~ :/– Sébastien
May 22 '13 at 13:24
@Sebastien Try now, I think that should fix that.
– Chris Down
May 22 '13 at 14:20
@Sebastien Try now, I think that should fix that.
– Chris Down
May 22 '13 at 14:20
@Chris, indeed adding a check on
[ -z "$1" ] was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution for zsh.– Sébastien
May 23 '13 at 8:18
@Chris, indeed adding a check on
[ -z "$1" ] was sufficient. I have accepted Stephane's answer to give it more visibility, as it seems to be the best solution for zsh.– Sébastien
May 23 '13 at 8:18
add a comment |
up vote
7
down vote
You could use dirname to strip the filename from the path, e.g.
mycd() cd "$(dirname "$1")";
See man dirname.
3
Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
– Chris Down
May 17 '13 at 18:08
1
@ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
– Adrian Frühwirth
May 17 '13 at 18:10
1
@AdrianFrühwirth An exercise! Hah!
– user13742
May 17 '13 at 18:24
add a comment |
up vote
7
down vote
You could use dirname to strip the filename from the path, e.g.
mycd() cd "$(dirname "$1")";
See man dirname.
3
Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
– Chris Down
May 17 '13 at 18:08
1
@ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
– Adrian Frühwirth
May 17 '13 at 18:10
1
@AdrianFrühwirth An exercise! Hah!
– user13742
May 17 '13 at 18:24
add a comment |
up vote
7
down vote
up vote
7
down vote
You could use dirname to strip the filename from the path, e.g.
mycd() cd "$(dirname "$1")";
See man dirname.
You could use dirname to strip the filename from the path, e.g.
mycd() cd "$(dirname "$1")";
See man dirname.
answered May 17 '13 at 16:44
Adrian Frühwirth
1,6191014
1,6191014
3
Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
– Chris Down
May 17 '13 at 18:08
1
@ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
– Adrian Frühwirth
May 17 '13 at 18:10
1
@AdrianFrühwirth An exercise! Hah!
– user13742
May 17 '13 at 18:24
add a comment |
3
Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
– Chris Down
May 17 '13 at 18:08
1
@ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
– Adrian Frühwirth
May 17 '13 at 18:10
1
@AdrianFrühwirth An exercise! Hah!
– user13742
May 17 '13 at 18:24
3
3
Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
– Chris Down
May 17 '13 at 18:08
Note that if you input a directory, this will change to the directory above that directory, which may not be desired (it's unclear from the question whether this is desired or not).
– Chris Down
May 17 '13 at 18:08
1
1
@ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
– Adrian Frühwirth
May 17 '13 at 18:10
@ChrisDown True, but I left that as an exercise to the reader and your answer shows how to deal with it.
– Adrian Frühwirth
May 17 '13 at 18:10
1
1
@AdrianFrühwirth An exercise! Hah!
– user13742
May 17 '13 at 18:24
@AdrianFrühwirth An exercise! Hah!
– user13742
May 17 '13 at 18:24
add a comment |
up vote
3
down vote
If you add this to your .profile, then load it (source ~/.profile or log out and log in again), then mycd [file or directory] will take you to the right directory:
mycd() if [ -d "$1" ]; then cd "$1"; else cd "$( dirname "$1" )"; fi ;
If you name it cd, then strange things will happen.
1
This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
– Chris Down
May 17 '13 at 17:38
@ChrisDown, though that's true of other shells, you don't need the closing semicolon inzsh.
– Stéphane Chazelas
May 18 '13 at 9:42
Also, this breaks if passing args tocd, like-P.
– Chris Down
May 18 '13 at 11:37
add a comment |
up vote
3
down vote
If you add this to your .profile, then load it (source ~/.profile or log out and log in again), then mycd [file or directory] will take you to the right directory:
mycd() if [ -d "$1" ]; then cd "$1"; else cd "$( dirname "$1" )"; fi ;
If you name it cd, then strange things will happen.
1
This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
– Chris Down
May 17 '13 at 17:38
@ChrisDown, though that's true of other shells, you don't need the closing semicolon inzsh.
– Stéphane Chazelas
May 18 '13 at 9:42
Also, this breaks if passing args tocd, like-P.
– Chris Down
May 18 '13 at 11:37
add a comment |
up vote
3
down vote
up vote
3
down vote
If you add this to your .profile, then load it (source ~/.profile or log out and log in again), then mycd [file or directory] will take you to the right directory:
mycd() if [ -d "$1" ]; then cd "$1"; else cd "$( dirname "$1" )"; fi ;
If you name it cd, then strange things will happen.
If you add this to your .profile, then load it (source ~/.profile or log out and log in again), then mycd [file or directory] will take you to the right directory:
mycd() if [ -d "$1" ]; then cd "$1"; else cd "$( dirname "$1" )"; fi ;
If you name it cd, then strange things will happen.
edited May 17 '13 at 17:38
Chris Down
78.5k13187200
78.5k13187200
answered May 17 '13 at 17:13
jwhitlock
42644
42644
1
This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
– Chris Down
May 17 '13 at 17:38
@ChrisDown, though that's true of other shells, you don't need the closing semicolon inzsh.
– Stéphane Chazelas
May 18 '13 at 9:42
Also, this breaks if passing args tocd, like-P.
– Chris Down
May 18 '13 at 11:37
add a comment |
1
This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
– Chris Down
May 17 '13 at 17:38
@ChrisDown, though that's true of other shells, you don't need the closing semicolon inzsh.
– Stéphane Chazelas
May 18 '13 at 9:42
Also, this breaks if passing args tocd, like-P.
– Chris Down
May 18 '13 at 11:37
1
1
This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
– Chris Down
May 17 '13 at 17:38
This will break if the directory name contains whitespace, and you need a closing semicolon to terminate the command group. Edited it for you, hope you don't mind.
– Chris Down
May 17 '13 at 17:38
@ChrisDown, though that's true of other shells, you don't need the closing semicolon in
zsh.– Stéphane Chazelas
May 18 '13 at 9:42
@ChrisDown, though that's true of other shells, you don't need the closing semicolon in
zsh.– Stéphane Chazelas
May 18 '13 at 9:42
Also, this breaks if passing args to
cd, like -P.– Chris Down
May 18 '13 at 11:37
Also, this breaks if passing args to
cd, like -P.– Chris Down
May 18 '13 at 11:37
add a comment |
up vote
0
down vote
cd2()
arg=() dir= cmd= IFS=" " msg='[-L
Given a directory called$(sudo reboot), this function may reboot the system. Also, the user is usingzsh, notbash.
– Kusalananda
Dec 2 at 9:34
Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
– ecwpz91
Dec 3 at 23:57
No,cd '$(sudo reboot)'would change directory, while your function would try to evaluate the name.
– Kusalananda
Dec 4 at 6:17
Ah, I see and made changes. Let me know if that works? Good catch.
– ecwpz91
Dec 5 at 7:21
add a comment |
up vote
0
down vote
cd2()
arg=() dir= cmd= IFS=" " msg='[-L
Given a directory called$(sudo reboot), this function may reboot the system. Also, the user is usingzsh, notbash.
– Kusalananda
Dec 2 at 9:34
Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
– ecwpz91
Dec 3 at 23:57
No,cd '$(sudo reboot)'would change directory, while your function would try to evaluate the name.
– Kusalananda
Dec 4 at 6:17
Ah, I see and made changes. Let me know if that works? Good catch.
– ecwpz91
Dec 5 at 7:21
add a comment |
up vote
0
down vote
up vote
0
down vote
cd2()
arg=() dir= cmd= IFS=" " msg='[-L
cd2()
arg=() dir= cmd= IFS=" " msg='[-L
edited Dec 5 at 7:19
answered Dec 2 at 1:27
ecwpz91
1012
1012
Given a directory called$(sudo reboot), this function may reboot the system. Also, the user is usingzsh, notbash.
– Kusalananda
Dec 2 at 9:34
Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
– ecwpz91
Dec 3 at 23:57
No,cd '$(sudo reboot)'would change directory, while your function would try to evaluate the name.
– Kusalananda
Dec 4 at 6:17
Ah, I see and made changes. Let me know if that works? Good catch.
– ecwpz91
Dec 5 at 7:21
add a comment |
Given a directory called$(sudo reboot), this function may reboot the system. Also, the user is usingzsh, notbash.
– Kusalananda
Dec 2 at 9:34
Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
– ecwpz91
Dec 3 at 23:57
No,cd '$(sudo reboot)'would change directory, while your function would try to evaluate the name.
– Kusalananda
Dec 4 at 6:17
Ah, I see and made changes. Let me know if that works? Good catch.
– ecwpz91
Dec 5 at 7:21
Given a directory called
$(sudo reboot), this function may reboot the system. Also, the user is using zsh, not bash.– Kusalananda
Dec 2 at 9:34
Given a directory called
$(sudo reboot), this function may reboot the system. Also, the user is using zsh, not bash.– Kusalananda
Dec 2 at 9:34
Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
– ecwpz91
Dec 3 at 23:57
Give any command $(sudo reboot), this function will reboot the system. Also, +1 I didn't notice 'zsh' initially - good catch.
– ecwpz91
Dec 3 at 23:57
No,
cd '$(sudo reboot)' would change directory, while your function would try to evaluate the name.– Kusalananda
Dec 4 at 6:17
No,
cd '$(sudo reboot)' would change directory, while your function would try to evaluate the name.– Kusalananda
Dec 4 at 6:17
Ah, I see and made changes. Let me know if that works? Good catch.
– ecwpz91
Dec 5 at 7:21
Ah, I see and made changes. Let me know if that works? Good catch.
– ecwpz91
Dec 5 at 7:21
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f76227%2fhow-to-make-cd-dir-filename-take-me-to-dir%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown