1

I want to have a script to call with params which adds some aliases to my current shell. The aliases have to be builded in respect to the calling parameters and the current directory from where I start it. If possible I would like to NOT have to source the script, just run it like any other script.

This is what I have done until now:

I wrote a small shell script to generate some aliases, like

echo 'alias foo="$1"'

and save it as file "myscript" with +x rights. Then I execute it via console to set the alias

$(myscipt hello)

that works fine

When I change my shell script like

echo 'alias foo="bar $1 -param"'

and execute again like above, the result is

bash: alias hello" not found
bash: alias -param" not found

???. I escaped already the blanks between bar and -param but can't get it work.

when I execute

source <(myscript hello)

everything is fine like expected (That is my solution by now but not sure if it is the best).

Some ideas how to get a better solution?

3
  • 1
    The answer to your question here is that source requires a file, which is what you get with process substitution <(), but not with command substitution $(). Your question however is a good example of an XY problem — usually it's better to ask about what you're in the first place trying to achieve rather than your attempted solution.
    – slhck
    Oct 15, 2014 at 15:46
  • @slhck - I updated my question - Maybe you have an better idea for my XY problem. Btw. when using command substituion $()I do NOT source the result. $() will inject the alias if there is NO space in the aliasing command. The question herefore is: What do I need to do if I want to use $() and spaces to separate arguments? Whatever - maybe you have a good hint for me. Thanks. Oct 15, 2014 at 16:36
  • Hopefully my answer guesses right, but in case it doesn't: Why is the source <(myscript hello) option not to your liking?
    – lindes-hw
    Oct 16, 2014 at 18:47

2 Answers 2

5

You're using the source command wrong. I'm assuming you have a file called myscript that contains these commands. Instead of having stuff to generate the right commands in the file, like this

echo 'alias foo="bar -param"'

you should just have the commands, like this

alias foo="bar -param"

Then you pass your script directly to the source command, which runs it in the current shell so that the changes made in the script are reflected in your current shell session:

source myscript

Also, the . command is equivalent:

. myscript

(Note that there's a space between the . and myscript.)


Update

In response to your comment: you should still just use a file. Since the name of an alias can be set using a variable value, you can just add the logic in the file to do all the dynamic aliases. Here's a super simple example:

$ foo=hello
$ alias $foo='echo hi'
$ hello
hi

You can use conditions, loops, etc to build up much more complex logic for your dynamic aliases.

6
  • Thanks, I know your answer but this is not what I am looking for. Have a look at my comment above, in a short: there is no static alias and I do not want to create a file. Oct 15, 2014 at 16:02
  • @TomFreudenberg - I still think the right way to do this is using a static file with the source command. See my edit above for details.
    – DaoWen
    Oct 15, 2014 at 16:20
  • Thanks again. But using a file I need to enter always 2 commands on shell. First: script to generate the alias temp file, Second: source temp file. In case that I am writing tools to a framework it is not a good solution to mee. Oct 15, 2014 at 16:27
  • I updated the question to be more precise what I am looking for. Thanks for your suggestions and help. Oct 15, 2014 at 16:29
  • @TomFreudenberg - You still seem confused about what I'm suggesting. You shouldn't need two scripts. Any "parameters" you have could probably be passed through environment variables. All the pwd logic should be handled within the static script.
    – DaoWen
    Oct 15, 2014 at 16:39
0

set -x turns out to be informative:

: $; set -x
: $; $(./alias-builder)
++ ./alias-builder
+ alias 'foo="bar' '$1' '-param"'
bash: alias: $1: not found
bash: alias: -param": not found
: $; 

So, what's going on here is that when bash is treating the output of alias-builder (which is the name I gave to a script containing your echo command) as something to substitute inline as arguments, it's separating the output on words, as might normally happen, but treating the quotes as part of the words, rather than what you're expecting, which is to parse the output the way it would as if it was typed.

I think ultimately your answer is to use the source option.

Now, if that's inconvenient to type, you could do something like this:

metamyscript() {
    source <(myscript "$@")
}

And then you should be able to do, simply:

: $; metamyscript hello

and it will run myscript hello, and source the output of that.

(Of course, you could switch the names around to your liking.)

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.