make alias available in sub-shells - UNIX - bash

How can I make all alias listed in
~/.alias
available in all sub-shells? I am new to unix so I don't know much about this.
Thanks

In your home directory there needs to be a .bashrc file You probably have one. For each alias add one line that looks like this:
alias[space]myalias[nospaceshere!]=[nospaceshere]'/path/to/command arg1 arg2 '
Example:
alias ll='ls -l '
You can add a trailing space if you feel one may prevent problems in the use of your alias.

In Ubuntu, you can write in ~/.bash_aliases:
. ~/.alias
Every time you open a new terminal, your aliases are defined.

Related

~/.zshrc alias with a space

I have a bad habit of putting spaces in my folder/file names. Today it bites me.
I have a folder called NFB Lab in which I installed NFB Lab. I wanted to add the shortcut/command nfb and pynfb to the ~/.zshrc file to start the main python script from anywhere.
I edited the ~/.zshrc file through nano with:
alias nfb=/Users/mathieu/Documents/NFB\ Lab/pynfb/main.py
alias pynfb=/Users/mathieu/Documents/NFB\ Lab/pynfb/main.py
I also tried:
alias nfb="/Users/mathieu/Documents/NFB Lab/pynfb/main.py"
alias pynfb="/Users/mathieu/Documents/NFB Lab/pynfb/main.py"
Neither works, I always get:
zsh: no such file or directory: /Users/mathieu/Documents/NFB
How can I solve this without uninstall/reintsall of NFB Lab?
You'll need to escape the space (\ ), for example, take a look at my sublimetext3 alias;
alias sub='/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl'
Otherwise, take a look at ZSH functions. There are many more options compared to aliasses;
For example, run python script with arg as path, then create an alias calling that function
function runpy() {
python3 "$#"
}
alias runx="runpy '/tmp/dir with space/py.py'"
alias runy="runpy '/tmp/dir with space/py_second.py'"
You need two backslashes.
$ mkdir "f oo"
$ alias f="cd f\\ oo"
$ f
$ pwd
/home/foobar/tmp/f oo
Considering the confusion caused by backslash inside single or double quotes, here is one alternative :
alias nfb="'/Users/mathieu/Documents/NFB Lab/pynfb/main.py'"
alias pynfb="'/Users/mathieu/Documents/NFB Lab/pynfb/main.py'"

How to set part of a path dynamically for alias command in bashrc file

I'm trying to have a alias command and the part of its path keeps changing.
Eg:
/var/mywork/swag/wsnew/
/var/mywork/swag/ws/
/var/mywork/swag/wsold/
and my alias command to achieve is something link this
alias cws='cd /var/mywork/swag//
since last directory in the path is keep changing. I wanted to get automatically update in alias command. Is there anyway ?
I tried something like
alias cws='cd /var/mywork/swag/getenv("WSP")/
so whenever I set WSP to required path it automatically takes. But it didn't help.
Like most problems with aliases, this is easier to solve with a shell function:
cws() {
cd /var/mywork/swag/"$WSP"
}
Like melpomene said, you are better off using a function, but if you, for whatever reason, want to stick with an alias, you can do
alias cws='cd /var/mywork/swag/$WSP'
But note that this differs from using a shell function in one point, which is related to setting a variable for just one command: If you write
WSP=aaa
...
WSP=xxx
...
WSP=yyy cws
and cws is defined as a function, this would cd to yyy, but if it is an alias, it would cd to xxx.

Create bash script with various alias?

I'm creating a script that when is run create various alias for various scripts that are in other folders.
The script and the other folders are inside a specific folder as shown in the image but it is gonna be executable only when I want too.
Assuming this is only gonna be to execute on this machine i don't have to change paths.
I got this in the script that runs perfectly, prints the echo and everything but the alias isn't created. Now if i just do the same alias line out of the script it creates the alias perfectly.
This script I'm creating is sh does it have any influence on this situation ?
Now i only want to use alias because this folder is going to stay in that machine and i'm not going to have other people running these.
What i want is to be able to instead of going to the folders and run the executables i want this script to create the alias so i can call them directly through the prompt like$~ zenmap and it runs.
#!/bin/bash
alias zenmap="/home/user/Desktop/folder/nmap/zenmap/zenmap"
echo "zenmap imported !"
Any clue on what can be happening ?
You should source your aliases script rather than simply running it. i.e.
source script.sh
or
. script.sh
From your comments in jayant answer it seems you are confusing when functions get executed. So a little example:
file_with_alias.sh
alias do_this="do_some_function"
" sourcing the file will make the function available but not execute it!
source file_with_function.sh
" This will only create the alias but not execute it.
alias execute_script="./path/to/script_that_does_something.sh"
file_with_function.sh
do_some_function(){
echo "look ma! i'm doing things!"
}
script_that_does_something.sh
echo "Doing something directly!"
Now when you source . file_with_alias.sh the function will not be executed, only the alias generated. You will need to execute the alias do_this or call the function for it to work.
$ source file_with_alias.sh
$ do_this
Look ma! I'm doing things!
$ execute_script
Doing something directly!

Shell script: shorten or aliasing an address after a command

I want to abbreviate or set an alias to a destination address every time I use while copying files. For example,
scp <myfile> my_destination
where my_destination could be hbaromega#192.168.1.100:Documents. So I want to modify my .bash_profile by inserting something like
alias my_destination = 'hbaromega#192.168.1.100:Documents' .
But that doesn't work since my_destination is not a command.
Is there a way out?
Note: I don't want to abbreviate the whole command, but only the address, so that I can use it with other possible commands.
You can't do what you want for the reason you state (an alias defines an entire command). But you could use a shell function to come close:
my_scp() {
scp "$#" hbaromega#192.168.1.100:Documents/.
}
which you could then call as
my_scp *.c
(Using $# in doublequotes is shell black magic that avoids trouble if any of the file names matched by the *.c glob contain spaces)
Of course, if you don't want to define a function, you could always use a shell variable to at least save the retyping:
dest='hbaromega#192.168.1.100:Documents/.'
scp *.c $dest
You have a couple options. You can set hostname aliases in your ~/.ssh/config like this:
Host my_destination
Hostname 192.168.1.100
User hbaromega
You could use it like this:
$ scp myfile my_destination:Documents/
Note that you'd still have to specify the default directory.
Another option would be to just put an environment variable in your ~/.bashrc:
export my_destination='hbaromega#192.168.1.100:Documents/'
Then you could use it like this:
$ scp myfile $my_destination
BertD's approach of defining a function would also work.
I think this works without using export as well since anyway I am assigning a variable for the path or destination. So I can just put the following in my .basrc or .bash_profile :
my_destination='hbaromega#192.168.1.100:Documents/'
Then
scp <myfile> $my_destination
Similarly I can execute any action (e.g. moving a file)for any local destination or directory:
local_dest='/Users/hbaromega/Documents/'
and then
mv <myfile> $local_dest
In summary, a destination address can be put as a variable, but not as a shell command or function.
The reason it does not work is there are spaces surrounding the = sign. As pointed out, an alias must be called as the first part of the command string. You are more likely to get the results you need by exporting my_destination and then calling it with a $. In ~/.bashrc:
export my_destination='hbaromega#192.168.1.100:Documents'
Then:
scp <myfile> $my_destination
Note: you will likely need to provide a full path to Documents in the export.

alias parameter not working

I am trying to make it easier to use scp so I learned about alias today, and I am using it like this:
alias loudie-scp="scp -i keys/aws.pem $1 ec2-user#ec2-107-20-68-112.compute-1.amazonaws.com:/home/ec2-user"
the $1 is there to specify the file i want to transfer over. However this is not working and giving me an error:
scp: /home/ec2-user: not a regular file
This does not happen when I execute this command manually passing in any file for $1.
BASH FAQ entry #80: "How can I make an alias that takes an argument?"
Unfortunately BASH aliases are kind of like find-and-replace -- they're not very powerful for the sort of task you describe. I would suggest using, instead, a script file and placing it in an executable directory; something like so:
#!/bin/bash
scp -i keys/aws.pem $1 ec2-user#ec2-107-20-68-112.compute-1.amazonaws.com:/home/ec2-user
Then, given that it has the name loudie-scp you could call it like so:
loudie-scp <parameter>
As I'm sure Ignacio's link will explain, an alias does nothing more than textually expand the alias to its value. It does not take arguments, you need to use a function for that.

Resources