Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

my code:

execInPath() {
prev_dir=${PWD##*/}
cd $1
shift
res=$($@)
cd prev_dir
echo res
}
alias path=execInPath

$ path ~ ls gives: bash: cd: prev_dir: No such file or directory (and the files in my home directory prior to that)

share|improve this question
    
cd $prev_dir instead of cd prev_dir; maybe also echo $res – Jeff Schaller Feb 2 at 16:51
up vote 3 down vote accepted

You must use "$prev_dir" to reference variable prev_dir:

execInPath() {
  prev_dir=${PWD##*/}
  cd -P -- "$1"
  shift
  res=$( "$@" )
  cd -- "$prev_dir"
  printf '%s\n' "$res"
}

alias path=execInPath

But using a subshell is easier:

execInPath() {
  : 'Change directory in subshell'
  (
    cd -- "$1" || return 1
    shift
    res=$( "$@" )
    printf '%s\n' "$res"
  )
  : 'Back to previous dir'
  pwd
}

alias path=execInPath
share|improve this answer

Use a subshell:

execInPath() (cd -P -- "$1" && shift && exec "$@")

Notes:

  • You need to check the exit status of cd as if cd fails, you'd run the command in the wrong directory.
  • if you want cd to behave like in other languages, you need -P.
  • think of the exit status of your function. Here, you want it to be unsuccessful if cd fails, and be the exit status of "$@" otherwise.
  • $@ must be quoted always.
  • It's impossible to do cd some-dir;...;cd original-dir and get back to the same original directory 100% reliably.
share|improve this answer
    
This also came into my mind at first, but it seems that the OP want to do other things inside function after go back to previous dir. – cuonglm Feb 2 at 17:13
    
Would you mind explaining more details about behave like in other languages, what's other languages? – cuonglm Feb 2 at 17:14
    
@cuonglm, the chdir or C, perl, python, ruby, java, you name it. – Stéphane Chazelas Feb 2 at 17:25

When you assign a variable in bash, there is no $ (unlike perl), but when you use/refer to a variable in bash, you need to add the $ to it. Your cd prev_dir should be cd $prev_dir.

share|improve this answer
    
cd $prev_dir doesn't make sense. It should be at least cd "$prev_dir" or rather cd -P -- "$prev_dir". – Stéphane Chazelas Feb 2 at 17:10
    
Not sure why it doesn't make sense. The quotes are not required. export prev_dir="/bin";cd $prev_dir works just fine. – Tim S. Feb 2 at 17:18
    
    
    
While I agree that it's a security issue, that's far from "doesn't make sense". Personally I don't use bash for a number of reasons, but that's beside the point. Thanks for the links. – Tim S. Feb 2 at 17:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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