I have a C++ program with a main routine which reads arguments from command line:

int main(int argc, const char *argv[])

In my system I have defined some aliases in my bash environment which abbreviates directory names.

alias dst='home/username/Documents/test'
alias src='home/username/Download/test'

My program is called "dircopy" and I want to use the following input:

dircopy /home/username/Download/test /home/username/Documents/test

But I want to avoid writing the whole path for destination directory and source directory each time I run the program. Is there a clever way to use the alias definitions in bash when calling my "dircopy"-program ?

(Example: dircopy $src $dst - or something similar...)

Edited by it@61@sec: spelling correction

3 Years
Discussion Span
Last Post by it@61@sec

You could use environment variables. In C/C++ you can access your environment either by the envp to main or through the getenv call. For getenv you might do something similar to the following pseudocode:

for arg in argv:
   if arg[0] == '$':
      char * variable = getenv(arg[1..-1])

You'd obviously need to add error checking to that but it should convey the process clear enough.


Seems OK, but I wonder if there is a way to make bash do the substitution BEFORE it lets the program process the input.
In that case I will not be forced to change my c++ program..


Yes, thank you sepp2k, I forgot to mention that $var in bash gets expanded prior to getting passed to your program.

With that in mind, my prior example is not too accurate since you wont likely see $ characters byt the time argv is filled in. Hopefully I haven't confused you too much.


Thanks to both of you. This was exactly what I was looking for..
(I'm a newbie on this.. :-) )

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.