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.
Example:

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...)

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..

If you do make them variables, dircopy $src $dst will work fine.

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.. :-) )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.