2

Is there a way to substitute aliases such that it is appended or prepended to an existing command in the shell?

For example, defining the alias:

alias execloc='/home/user/'

Prepending this to a command in the bash terminal:

cd exeloc/temp/somefolder

Can something like this be done, or is there a way to do it?

2

You can't do it like this because /home/user/ is not a command. It't more like a static string.

From man bash (somewhere at line 1984):

Aliases allow a string to be substituted for a word when it is used as the first word of a simple command.

In your case, execloc will never be the first word of a simple command.

But, instead to define execloc as an alias, you can define it as an environment variable:

export execloc='/home/user/'

And then you can use it everywhere you want as follow:

cd $exeloc/temp/somefolder
0

I don't think so with bash/dash, but I could be wrong. I think it may be possible with 'zsh' (alias -g foo='bar').

But there are some other possibilities.

If you are just wanting more convenient 'cd' commands, you could use CDPATH (google it). Or use symlinks or hardlinks (man ln).

Or modify/create ~/.inputrc with a line like:

Control-f: "grep -r -i .\e[D\e[D\e[D"

This types out a grep command for me and then backspaces to before the dot so I can type ctrl-f and then a word I'm searching for.

0

You can do it (if I read your question properly) as follows:

export EXECLOG=/home/user

cd $EXECLOG/temp/somefolder

Basically set up your substitutions in the shell and access them by prefixing them with the "$" sign. Standard shell variables.

(exited to fix typo in EXECLOG)

  • Have you seen this answer? – Radu Rădeanu Oct 24 '13 at 7:04
  • Yes, I have - have you tried to run what I suggested? The substitution is done in the shell and will perform the example the OP posted. – FreudianSlip Oct 24 '13 at 7:09
  • Ah, now I see - your post contains what I posted as well - so I duplicated your answer. SO although I did see your response, I didn't read it all the way through. Take off the downvote and I'll remove the response. – FreudianSlip Oct 24 '13 at 7:13

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.