Alias substitution for a string to use it in a terminal command

9,026

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
Share:
9,026

Related videos on Youtube

Stephen Jacob
Author by

Stephen Jacob

A Civil and Structural Engineer with background in application of High Performance Computing in evacuation from Tsunami with cities damaged by earthquake. Currently, trying to hone my skills in Civil and Structural engineering, and Machine learning using Python.

Updated on September 18, 2022

Comments

  • Stephen Jacob
    Stephen Jacob almost 2 years

    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?

  • Radu Rădeanu
    Radu Rădeanu over 10 years
    Have you seen this answer?
  • FreudianSlip
    FreudianSlip over 10 years
    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
    FreudianSlip over 10 years
    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.