2

I have some scripts (that are out of my control) that loads some environment settings to my session. So whenever I want to load I have to execute a series of commands:

export SOME_VAR=/path/to/main/folder
source $SOME_VAR/loading/stuff.sh --quiet
loadApp1
loadApp2
loadApp3
loadApp4

I want to do this with a single command, so I created a function (pretty much the lines above), but it doesn't work.

I think it's because the loadApps are aliases, loaded in the stuff.sh so at time Bash parses my functions it doesn't know the aliases yet.

How could I do this with a single command without losing portability?

PS: I do not want to copy the alias to my $HOME/.bashrc because they can be changed elsewhere and I won't see it.

  • How are you executing stuff.sh? Are you using . to run in in your current shell, or something like ./stuff.sh to run it in a different shell? If it runs in a separate shell, any aliases or environment variables it sets up will have no effect on your current shell. See stackoverflow.com/questions/2197461/… – wingedsubmariner Oct 29 '13 at 13:24
  • Please include your actual code in your questions. You wrote "pretty much the lines above" which is worrying. Why not show us the actual function you are using so we can check for errors and the like? – terdon Oct 29 '13 at 13:58
  • @wingedsubmariner I'm sourcing, as explained in the question – RSFalcon7 Oct 29 '13 at 14:08
  • @terdon I show the code I repeated type in the me terminal, the function is precisely the same thing with function func_name() { and } around, I tough this would be redundant – RSFalcon7 Oct 29 '13 at 14:10
  • 1
    @RSFalcon7 - you're quite welcome, and thanks for the Q. – slm Oct 29 '13 at 14:59
7

If they are aliases, you'll have to write it:

myFunction() {
  export SOME_VAR=/path/to/main/folder
  . "$SOME_VAR/loading/stuff.sh" --quiet
  eval '
  loadApp1
  loadApp2
  loadApp3
  loadApp4'
}

aliases are expanded at the time they are read/parsed. eval forces such a parsing.

3

I would use a script to do this just to be on he safe side:

#!/usr/bin/env bash
export SOME_VAR=/path/to/main/folder
source $SOME_VAR/loading/stuff.sh --quiet
loadApp1
loadApp2
loadApp3
loadApp4

Just save that in your $PATH, make it executable and run it instead of your function.

2

There's no problem declaring a function containing unknown commands. It's not until you try to execute it that trouble occurs.

$ unknown() { foo; bar; baz; }
$ unknown
bash: foo: command not found
bash: bar: command not found
bash: baz: command not found
  • yes, the problem is executing the function – RSFalcon7 Oct 29 '13 at 14:08
  • @RSFalcon7: ... use it only after you defined the aliases? (I'm probably missing something, here, but I think you need to first define them before you use that "unknown ()" function at all? the function can be defined before, but of course will only know 'loadAppN' commands if by the time you use it those commands are defined...) – Olivier Dulac Oct 29 '13 at 18:43
  • the function loads the aliases with a source command and then use them, but at the time bash define the function those commands aren't defined so issues an error, as far as I learned today eval forces bash to define the command in run-time – RSFalcon7 Oct 29 '13 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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