Reputation: 396
I'm trying to use an alias in a shell script I'm writing, but it is not working.
The alias:
alias ts="awk '{ print strftime(\"[%Y-%m-%d %H:%M:%S]\"), \$0 }'"
When I run the script, I get the following error:
./copyTask.sh: ts: not found
Sooping around on the internet, it seems that I need to enable the expand_aliases
shell option, but I don't have shopt
installed... Is there any way I can enable alias expansion without using shopt
or creating another rootfs image?
I'm using the ash
shell. And awk
is BusyBox v1.25.0 awk
.
NOTE: The alias is an easy way to prepend a timestamp to a commmand:
$ echo "foo" | ts
[2005-06-23 11:52:32] foo
EDIT: as some people are having trouble understanding what I mean, this answer has an example.
Upvotes: 1
Views: 732
Reputation: 295687
Don't use aliases in scripts. A function does the same job better.
ts() { gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' "$@"; }
export -f ts
will make the ts
command available to subprocesses (where the shell they run is also bash).Upvotes: 3