简体   繁体   中英

Bash: use an alias in a filepath

I cannot seem to figure out how to use an alias for a filename in a path. For example:

alias a="ls /tmp/ -tr | tail -n 1"
cat /tmp/a

The alias gives me the name of the most recent file written in /tmp/ . Then I try to cat that file and I get cat: /tmp/a: No such file or directory .

When I just type a I get the right thing, for instance some_log_I_just_wrote_to.log .

Obviously what I want is for cat /tmp/a to be translated into cat /tmp/some_log_I_just_wrote_to.log so that I can see the log.

Any ideas on what the right way to do this is? I sometimes get tangled up in bash with when to use aliases and symlinks and just plain variables, etc.

Thanks!

Create the alias with

alias a='ls /tmp/ -tr | tail -n 1'

Use the alias with cat

cat $(echo -n /tmp/ && a)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM