Skip to content

Commit

Permalink
interp: support "cd -" as "cd $OLDPWD"
Browse files Browse the repository at this point in the history
  • Loading branch information
donorp authored Dec 2, 2021
1 parent e482fb8 commit 1963ae1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions interp/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ func (r *Runner) builtinCode(ctx context.Context, pos syntax.Pos, name string, a
path = r.envGet("HOME")
case 1:
path = args[0]

// replicate the commonly implemented behavior of `cd -`
// ref: https://www.man7.org/linux/man-pages/man1/cd.1p.html#OPERANDS
if path == "-" {
path = r.envGet("OLDPWD")
}
default:
r.errf("usage: cd [dir]\n")
return 2
Expand Down
4 changes: 4 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,10 @@ var runTests = []runTest{
`mkdir a; ln -s a b; [[ "$(cd a && pwd -P)" == "$(cd b && pwd -L)" ]]; echo $?`,
"1\n",
},
{
`orig="$PWD"; mkdir a; cd a; cd -; [[ "$PWD" == "$orig" ]]`,
"",
},

// dirs/pushd/popd
{"set -- $(dirs); echo $# ${#DIRSTACK[@]}", "1 1\n"},
Expand Down

0 comments on commit 1963ae1

Please sign in to comment.