How to set the following in .cshrc for aliasing - linux

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

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

Is it possible to use custom alias for mathematica run?

This is a general question on the use of alias command in linux, but I will take with a mathematica example to be more clear.
I want to creat an alias for mathematica run.
The mathematica run command for a mathematica file myfile1.m is
math -run "<<myfile1.m"
Now if I put this whole command as an alias in bashrc like
alias m='math -run "<<myfile1.m"'
it will run the file when I just type m in terminal.
But I want to know whether there is a way to use in the following way so that it could be used for any mathematica file run in a more sophisticated way :
alias m='math -run "<<file.m? "'
so that from the terminal I can run different mathematica files just typing
m myfile1.m
it will run
math -run "<<myfile1.m"
similarly for anyfile.m one just types
m anyfile.m
and it will run
math -run "<<anyfile.m"
I'd suggest to do something like:
alias m='math -run'
so the command would look like
m "<<anyfile.m"
which is look better, as for me. If you'd like to get deeper, look into this:
Link
With #Bandydan's suggestion the following seems to work.
function m() { math -run "<<$#" ;}

Define alias that references other aliases

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'

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.............

Set Enviroment Variable that contains two paths interelated paths

I am trying to create a custom environment variable that uses python to execute a py file.
Here is an example of what I have
export VAR=${VAR}:"/usr/bin/python2.7 /home/user/file"
When I use the variable I get the output:
bash: :/usr/bin/python2.7: No such file or directory
If I echo the variable I get the output:
/usr/bin/python2.7 /home/user/file
EDIT:
Trying "$VAR" gives me the output
bash: :/usr/bin/python2.7 /home/user/file: No such file or directory
If I run just this /usr/bin/python2.7 /home/user/file it works
I think an alias is more appropriate for all kinds like this (you may consider a more suitable name for the alias)
alias var="/usr/bin/python2.7 /home/user/file"
If you want to stick with your version you have to tell your shell to evaluate the content of VAR.
For this you just have to invoke
eval ${VAR}
By the way, why do you append the string "/usr/bin/python2.7 /home/user/file" to VAR instead of overwriting the content of VAR?

Resources