Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 10240  / 2 Years ago, fri, may 6, 2022, 6:05:10

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?


More From » bash

 Answers
6

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

[#28780] Saturday, May 7, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
uquelighted

Total Points: 242
Total Questions: 110
Total Answers: 106

Location: Cyprus
Member since Tue, Sep 22, 2020
4 Years ago
uquelighted questions
;