1

I have the following two functions defined in my .bash_functions (which gets sourced into .bashrc):

up() {
  if (($# == 0)); then
    cd ..
  else
    for basename; do
      local result=$(_foo)
      echo $result
      cd $result
    done
  fi
}

_foo() {
  echo ${PWD%/$1/*}/$basename
}

While I can execute _foo, when i execute up, up doesn't seem to know _foo. Am I doing something wrong here, or is this just not possible?

1
  • Where do you think foo is getting that $1 from? The function gets its own parameters from the call to foo, not some magical global area. Commented May 10, 2024 at 3:11

2 Answers 2

2

It does "know" _foo, but you don't pass a parameter to _foo, so probably that's causing the confusion.

Sign up to request clarification or add additional context in comments.

1 Comment

It sees basename just fine. Variables are global unless declared local. The problem is that _foo() expects an argument ($1) and isn't getting passed one. This is what your example is actually illustrating, by the way.
1

Bash scripts are executed sequentially. In your case, _foo() could be defined before up() and everything should work fine.

1 Comment

It doesn't matter what order they're defined as long as they're called after they're defined. up() { _foo; }; _foo() { echo "bar"; }; up works just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.