2

I am using bash shell in Unix and within bash profile [.bashrc] I have created few aliases to navigate to different directories. My requirement is to pass that alias name as parameter to a function and use alias to navigate to that directory inside the function.

So I have following things inside .bashrc:

alias nav='cd /app/nav/'

function test(){
    $1;
}

Now I run this command test nav but the alias doesn't work inside the function. Any suggestions on how to get this going?

2
  • 1
    Investigate CDPATH; make sure you set it so that the first directory is the current directory CDPATH=:/app. Now you can just type cd nav and it will take you to /app/nav unless you happen to have a sub-directory nav in the current directory. When it uses CDPATH, the shell tells you which directory it changed to. Commented Sep 10, 2013 at 14:59
  • 1
    If you used functions instead of aliases, then you would have no problem passing them as arguments. It's just as easy: nav() { cd /app/nav; } (one character shorter, in fact). Commented Sep 10, 2013 at 15:44

2 Answers 2

4

You will need the dangerous eval--dangerous because it might do something that unexpected such as:

test "rm -fr /"

Also, please do not call your function test, which is a reserved word. Here is my solution:

function doalias() {
    if alias $1 2> /dev/null; then
        eval $1
    fi
}

Now, you can invoke it as:

doalias nav

Discussion

  • Your function might simply be eval $1 and be done with it, but I added a safeguard to test the first parameter to see if it is an alias and only call eval upon true.
  • I don't want the output of the alias command, so I hid it by mean of 2> /dev/null
Sign up to request clarification or add additional context in comments.

1 Comment

Didn't know much about aliases, and it seems eval could invoke it. +1 :)
1

It's better if you declare them as functions instead:

function nav {
    cd /app/nav
}

function test {
    echo "Testing function $*."  ## Optional message.
    "$@"
}

Example run:

> test nav
Testing function nav.
bash: cd: /app/nav: No such file or directory

It's an error, but it shows that nav is executed inside test function.

Also I suggest using a different name besides test since test itself is a builtin. See help test.

2 Comments

In my case, function nav {..} results in: "-bash: /etc/.bash_aliases: Zeile 10: Syntaxfehler beim unerwarteten Wort »}«" Whereas with function nav(){..} it works. First line of the file is "#!/bin/bash" .Any help?
@Timo Was there any other difference between the two functions besides ()? Perhaps place them on pastebin.com? Also, consider the shell you're using to execute it. Make sure it's bash and check the version as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.