I am trying to create bash scripts. I would like them to run in my current shell so that, for instance, when I create a directory I want to be redirected into the new directory without having to type cd and the path of the new directory.
All my scripts are saved in a bin folder in my home directory.
This is an example of a bash script called test.sh:
#!/bin/bash
mkdir /path/of/the/directory
cd /path/of/the/directory
Is it a good practice to create an alias and use source command in the alias like below?
alias ="source $HOME/bin/test.sh"
Thank you very much in advance for your help!!!
Thats absolutely fine. For example, I have an alias in my .bashrc that sources .bashrc:
alias rebash='source ~/.bashrc'
No, there is absolutely no reason to do that.
This sounds vaguely like you should be creating a function which contains the code, and not have an alias or an external file at all.
g () {
mkdir -p /path/of/the/directory
cd /path/of/the/directory
}
Put this in your .bashrc or similar. Maybe if you want it in a separate file, create a file $HOME/bin/interactive.bash and then just source $HOME/bin/interactive.bash from your .bashrc.
It is fine if you use source in an alias.
However, as a general practice, you should define all your aliases in ~/.bash_aliases and source them in ~/.bash_profile using source ~/.bash_aliases so that once a new shell is launched it will load all the available aliases.
Related
I have a bash script, which I use for configuration of different parameters in text files in my wireless access media server.
The script is located in one directory, and because I do all of configurations using putty, I have to either use the full path of the file or move to the directory that contains the file. I would like to avoid this.
Is it possible to save the bash script in or edit the bash script so that I can run it as command, for example as cp or ls commands?
The script needs to be executable, with:
chmod +x scriptname
(or similar).
Also, you want the script to be located in a directory that is in your PATH.
To see your PATH use:
echo $PATH
Your choices are: to move (or link) the file into one of those directories, or to add the directory it is in to your PATH.
You can add a directory to your PATH with:
PATH=$PATH:/name/of/my/directory
and if you do this in the file $HOME/.bashrc it will happen for each of your shell's automatically.
You can place a softlink to the script under /usr/local/bin (Should be in $PATH like John said)
ln -s /path/to/script /usr/local/bin/scriptname
This should do the trick.
You can write a minimal wrapper in your home directory:
#!/bin/bash
exec /yourpath/yourfile.extension
And run your child script with this command ./NameOfYourScript
update: Unix hawks will probably say the first solution is a no-brainer because of the additional admin work it will load on you. Agreed, but on your requirements, my solution works :)
Otherwise, you can use an alias; you will have to amend your .bashrc
alias menu='bash /yourpath/menuScript.sh'
Another way is to run it with:
/bin/bash /path/to/script
Then the file doesn't need to be executable.
So,
cd .. moves back into the parent directory.
I'd like to add functionality so that I could type...
cd ...
... and move into the parent directory's parent directory.
and consequently move up an additional tier for each extra .
The idea came from an SO answer about a script called 'up' which should do essentially the same thing. But I'm curious if it'd be possible to just add to the cd command.
After a quick search I've noticed that cd is a bash builtin so I don't think it'll be possible to edit any original code. Would it be possible to create a new cd(.sh) script that executes in place of the builtin cd command when valid arguments are provided? What other ways might this be accomplished by?
Note: this is more for learning than practical application, I just think it'd be a cool thing to do.
Thanks!
You can define an alias in your .bashrc file:
alias ...='cd ../..'
and after that you can issue ... to go up two directories. If you only want that alias for cd it will work.
You can add the following lines to the file ~/.bashrc
alias cd..='cd ..'
alias cd...='cd ../..'
and so on.
After adding this lines close the terminal and open an new one. There you can us cd.. to go one directory up, cd... to go two directories up ...
Let's say I have a simple function serving as an alias to cd.
c(){ cd "$#"; }
How can I get the original autocompletion of cd work for my new function? Note that cd only autocompletes directory names, not simple file names.
I know about the complete command, but where exactly is the autocompletion function used by cd located?
Thanks for help!
Look for /etc/bash_completion or /etc/bash_completion.d for the system-wide defaults.
How can I define an alias so that when I do cd Abcd, where 'Abcd' is the name of a directory, the directory is changed to 'Abcd' and is followed by ls to show the contents of the directory?
I believe you can't use an alias to accomplish that, but you could define a function to do it:
#print contents after moving to given directory
cl()
{
cd $#
ls
}
You could stick this in your ~/.bashrc file.
If you were hoping to override the builtin cd command, then you could do:
#print contents after moving to given directory
cd()
{
builtin cd $#
ls
}
UNIX
Creating an alias
Your Linux distribution will most likely not have the .bash_aliases file created in your home, or you can even create it manually. To create the file, type in the following command:
touch ~/.bash_alisaes
Now that file will be executed automatically every time you fire off a new Terminal.
What you can do now is create a list of aliases and add them to that file for later uses.
create an alias and update the ~/.bash_aliases file to make it permanent.
Generic approach: Creating a custom script
Create a bash script in your /usr/bin folder, it should look something like this
#!/bin/bash
Whatever combination of commands you want to run when you type this thing.
Its really that easy.
Just name the bash script what you want to type in to the terminal, and make it excecutable: chmod +x filename and you're good to go!
WINDOWS
You can use DOSKEY command:
From Wikipedia:
DOSKey is a utility for MS-DOS and Microsoft Windows that adds command
history, macro functionality, and improved editing features to the
command line interpretersCOMMAND.COM and cmd.exe. It was included as a
TSR program with MS-DOS and PC-DOS versions 5 and later, and with
Microsoft's Windows 95/98/Me.
For example: To create a macro that quickly and unconditionally formats a disk, type:
doskey qf=format $1 /q /u
To quickly and unconditionally format a disk in drive Z, type:
qf Z:
To define a macro with multiple commands, use $t to separate commands, so the solution to your problem follows:
doskey cd=cd $1$tdir
Now, this will work only in your currently open command window. To make it permanent simply create a batch file and set the value of the absolute path of the file to the regedit
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
Source for the regedit: superuser.com/a/238858
I'm using a command which I don't know where the information is stored.
alias nup='ps ax | grep "nginx"'
Where is this alias saved?
It depends upon your environment and configurations.
For bash, I would generally put it in a .bashrc file that in a home directory.
In ubuntu alias get stored in the .bashrc file.
If you are typing alias update_linux='sudo apt-get update' in the terminal, then it will create an alias temporarily. It works until you close your terminal.
To add an alias permanently you can edit ~/.bashrc and add the alias to it:
gedit ~/.bashrc
and add alias at the end
alias update_linux='sudo apt-get update'
Don't forget to refresh the .bashrc configuration, by running:
source ~/.bashrc
for more details on creating alias you can read following blog: Codebucket.
Try
grep alias ~/.*
grep alias /etc/*
to find most aliases. In /etc/default, /etc/environment, depending on your distribution (I read: ubuntu)/version there might be more in other /etc/ -subdirs.
I am using Ubuntu 14.04, and you may put your aliases directly in .bashrc, but you may also create a file in ~/.bash_aliases, which will hold your aliases separately and load them automatically.
By default, the .bash_aliases file is not there. You will need to create it, but first make sure you create it in the same directory as your .bashrc file
To find your .bashrc, you may use this:
sudo find / -name .bashrc -print
My output was:
/root/.bashrc
/home/ddropik/.bashrc
/etc/skel/.bashrc
As mentioned by OddityOverseer and ranendra, I am probably interested in the one in my home directory, that is /home/ddropik/.bashrc. So I navigate to my home directory, cd ~/
Now create the .bash_aliases file with touch .bash_aliases and then edit it with nano .bash_aliases. Add whatever aliases you want.
You won't be able to use your newly added aliases until you open a new terminal session, or reload your profile, --bash login
It's ussually in a file in your home directory, such as .aliases or something.
The question here is:
if I have an alias named 'shortcut,' how do I find out what file is defining that as an alias?
The best and most user-friendly way to do this is this:
sudo grep -roI alias\ nameOfAliasHere=\' /etc/ /home/yourUserName/
-r tells grep to read everything in the directory, and go through the directories recursively, Without it, grep will complain about what you want to do.
-o means print only the part of the line that matches your string
-I suppresses binary files in the results, because those will not help you find out where your alias is
The backslashes mean "treat the next character as part of the string, instead of the normal way you interpret it"
The command will go through everything, including subdirectories, in your home folder and in /etc
If you want to start with the most likely places, just do your home directory:
sudo grep -roI alias\ nameOfAliasHere=\' /home/yourUserName/
To search everywhere it's likely to be defined or mentioned, which can be handy, this:
sudo grep -oIr alias\ ls=\' / --exclude-dir={sys,proc,srv,media,tmp,sbin,bin,boot,mnt,recovery,run,backups,var}
A lot of things like to make the 'ls' command fancier, for example. Check out the comparison below. I also included 'time' at the beginning for kicks:
You can see that there are a couple places outside of your home dir and /etc that have that alias, and it's also defined in both .alias and .bashrc. Personally, I like to throw my custom aliases in a file called .alias, and then tell everything to source it. If you're having trouble with an alias you're trying to define, that's handy. The things you see in the ~/Downloads and .cache directories won't affect your active aliases. Same with the /usr directory.
The file in /etc/skel is used to create home directories for new users, so anything there doesn't affect you. If something shows up in /etc/profile though, that will.
You can also see that the root user has an alias for ls.