Define alias that references other aliases - linux

I'd need to be able to define an alias in a Debian shell that includes other aliases, for a project I'm currently working on.
Let's look at a code example to make things more clear.
alias foo=foo
alias bar=bar
If I run type foo it returns foo is aliased to 'foo', and type bar returns bar is aliased to 'bar'. Up to here all fine. Now, where I'm having the problem.
alias foobar=$foo$bar
Doesn't work. type foobar returns foobar is aliased to ''.
I've tried alias foobar=${foo}${bar} and it doesn't work either.
Once I get this working, in the final version I actually need some text in between the two aliases, imagine something like: alias fooandbar=${foo}and${bar}.
Can anyone help please?

To reuse alias in another alias use:
foobar='foo;bar'
However I would suggest you to consider using shell function to get better control over this.

Following #anubhava's sage advice:
foo() { echo foo; }
bar() { echo bar; }
foobar() { echo "$(foo)$(bar)"; }
fooandbar() { echo "$(foo)and$(bar)"; }
Spaces and semicolons inside {} are required there.

Here is an example of alias that i'm using
#clear
alias cls='clear; ls'
alias ccls='cd; cls' # used alias cls

I used this in csh and it worked for me :
alias new_alias 'vi old_alias'

Related

Use alias and add variable together

I found nice command to check whether in terminal:
curl wttr.in/
By adding after slash city name it will show whether in typed place for example:
curl wttr.in/NewYork
There is need to simplify this command using alias but then it appear a problem with variable after alias.
alias yyy="curl wttr.in/"
There is error trying to use alias with variable in terminal:
yyy NewYork
How manage to use alias with variable?
You can create a function and use it the same way you use an alias
function yyy() { curl wttr.in/${1}; }
yyy NewYork

How I can make alias called when it's called by a variable

I added an alias:
$ alias anyalias="echo kallel"
If I execute:
$ anyalias
kallel
it executes the echo command without any problem.
Now, if I define a variable in this way:
$ var="anyalias"
and then execute with this way:
$ $var
-ash: anyalias: not found
Then I got a shell error.
How I can make $var running the command defined in the anyalias alias?
I m not looking to change the way of calling $var. But I m looking for a way of changing the definition of the alias or export it.
Instead of alias consider using function:
anyfunc() { echo "kallel"; }
v=anyfunc
$v
kallel
Safer is to store the call of function in an array (will store arguments also, if needed):
var=(anyfunc)
"${var[#]}"
kallel
That's because alias expansion is performed previous to parameter expansion:
Command-line Processing
As you can see, you can go through the process again with eval, which is not recommended.
Instead, you can use some alternatives as the one by #anubhava.
Example
$ alias anyalias="echo kallel"
$ var=anyalias
$ $var
bash: anyalias: command not found
$ eval $var
kallel
Again, use eval carefully. It's just to illustrate the expansion process.

concatenating aliases in linux shell

I would like to concatenate aliases. So for example, if I have the following:
alias aliasone="cat"
alias aliastwo="/etc/passwd"
I would like to be able to type in the shell something like "aliasone+aliastwo" and then the following command will be executed:
cat /etc/passwd
Can this be done?
Thanks!
Aliases are only for command substitution. If you want to have shorthand for arguments, use shell variables.
file=/etc/passwd
cat "$file"
Simply remove the "alias" from the second line. That is:
alias aliasone="cat"
folder="/etc/passwd"
And then you can write:
aliasone $folder
The problem is that alias evaluate commands; but in the second alias there is no command. In the case of a parameter is better to use a variable. If you have a more particular situation (e.g. you are are inside a script) tell us so we can give a better solution.
I think you are trying to run several aliases at the same time, one after another.
I might be wrong, but if this is the case, the easy solution will be to use && in a new alias.
For example you have two existing ones:
alias cdtomydir='cd /home/mydir'
alias listfilesindir = 'll'
then you are able to combine the two above into a third alias by using &&:
alias cdtomydirandlistfiles = 'cdtomydir && listfilesindir'
you can do this: alias aliasone='cat /etc/passwd' then just type aliasone and that's it then if you're going to use cat looking elsewhere then type alias aliastwo='cat /etc/shadow' for example. Anyway just change path and that's it and make sure that aliases are different and keep on mind that words used for commands aren't reserved.
Step 1:
alias ccat='cat $1'
Step 2:
ccat /etc/passwd
Output>>
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin.............

Issue with alias in bash

I am facing a problem with alias command in bash. I created .bashrc in my home directory and added an alias as below:
test() { "test_command_$1"; }
alias au=test
But when I open a new terminal and try to execute the command through alias like au arg1 it complains:
bash: test_command_arg1: command not found
But if I execute aliased command manually in the same terminal as test_command_arg1 it is working fine. I checked the PATH variable and it's fine. Can somebody help me to fix this?
P.S: test_command_ is just an example. It is not the actual command being tried.
You are getting the "command not found" error because of what your test() function does.
test() { "test_command_$1"; }
When you call this with an argument, $1 is replaced with that argument (I guess you knew this bit). So if you call au arg1, what your function is trying to do is this:
"test_command_arg1"
The shell is trying to execute it as a command. test_command_arg1 isn't a recognised command, so you get an error. Try changing your test function:
test() { echo "test_command_$1"; }

How to set the following in .cshrc for aliasing

I have following alias in my .cshrc
alias fe-fat "source fat /prj/work"
alias fe-fat1 "source fat1 /prj/work"
I wanted to know if we can set some variable for fat/fat1 and club the above 2 aliases into a single alias somewhat like the below, so that whenever I type fe-<variable> in the Unix terminal the above alias should work
alias fe-<variable> "source <variable> /prj/work"
No, I don't think that's possible with alias.
What are you actually trying to achieve?
It'd be simple enough to write a bash script that launches other commands based on a parameter (so you'd type fe <variable> instead of fe-<variable>).
It's not really worth it with only 2 commands but you could also provide a custom auto-complete script: See Line completion with custom commands
Any particular reason you wouldn't just alias it to something that takes a parameter?
alias fe "source \!:1 /prj/work"
fe fat #=> source fat /prj/work
fe fat1 #=> source fat1 /prj/work

Resources