1

I have gotten the wrapper script 'hhighlighter' from https://github.com/paoloantinori/hhighlighter which colors anything you want in stdout. I followed the instructions about installing ack-grep, created alias for ack=ack-grep in ~/.bashrc, put the h() function in ~/.bashrc, everything works fine when typing in the terminal. But when putting the EXACT same commands in a bash script, it says command not found. I will give examples here...

root@kali:~# echo "abcd" | h a b c d
abcd

Here is what my script looks like

#!/bin/bash  
echo "abcd" | h a b c d

Exactly like the command typed in, but here is the output when running the script

root@kali:~# ./test.sh
./test.sh: line 1: h: command not found

How could this work when typing directly in the terminal, but not when having the exact same command in a bash script? If my ~/.bashrc is needed or anything else is needed to solve this please let me know, its been going on 4 days now without figuring this out.

2

The problem is: When bash is started non-interactively then it does not read ~/.bashrc.

Thus you have to read h() from the script.

If you want to use aliases in a script then you have to

  1. enable the use of aliases in the script with shopt -s expand_aliases

  2. define the alias in the script.

An alternative to an alias is defining a shell function (which works the same in interactive and non-interactive shells):

foo () {
  bar "$@"
}

But it is not possible to take over alias or function definitions from the running shell into a script as the shell running the script is re-initialized.

  • So should i put the h() function in my bash script instead of my ~/.bashrc file? The instructions here github.com/paoloantinori/hhighlighter say to put the h() function in the ~/.bashrc file..... – Will Hughes Nov 25 '14 at 18:34
  • I put the h() function in my bash script, and now its telling me the 'ack' command cannot be found. I have ack-grep installed for my distro not ack, and so I made an alias inside my ~/.bashrc -- alias ack='ack-grep'...I even typed it in the terminal before running the command but still says ack - command not found... – Will Hughes Nov 25 '14 at 18:40
  • @WillHughes I have extended the answer. – Hauke Laging Nov 25 '14 at 22:09
  • I reworded the question to hopefully make more sense, and simplify it a little. – Will Hughes Nov 25 '14 at 22:40
  • @WillHughes I have told you everything you need to know. Obviously you don't do what I explained in my answer. – Hauke Laging Nov 25 '14 at 22:44
0

Solution that helped me was to force the script to run in interactive mode with a #!/bin/bash -i header.

Advanced Bash-Scripting Guide warns about weird consequences though:

Be aware that this can cause erratic script behavior or show error messages even when no error is present.

However I haven't observed anything like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.