Create aliases through shell script - shell

We are using git for source control, which is installed on my windows machine.
In a nutshell I would like to create an alias for a shell script. I figure this out: alias ulf='. ulf.sh'. So far so good.
What I want is to add this to a script we have that configures all our git aliases (setaliases.sh). So far what I have read is that setting alias only applies to the current shell and when executing a shell script it does it in another shell. So I found out that you can call source ./setaliases.sh. This works right until I close down git-bash. Then I have to recreate the alias.
To sum it up: I want to create a global alias from within a .sh script.

You can input source .setalias.sh in your .bash_login file.
or another way to use alias is add some line in your .bashrc like this
alias ll='ls -lah'
alias gg='git status -s'
# to call more alias in setalias.sh file
source ~/.setalias.sh

Related

How to properly write a bash executable file

I am having some issue in writing a simple executable .sh file via bash.
The order of operation as soon as I open a terminal (ctrl+alt+T) are:
pc:~$ roscd
pc:~/catkin_docking_ws/devel$ cd ..
pc:~/catkin_docking_ws$ cd devel/lib/tuginterface/
pc:~/catkin_docking_ws/devel/lib/tuginterface$ ./tuginterface
I have been investigating this small issue and came across this source which advises to change and rename the project as an alias and that is exactly what I tried to do:
alias proj="roscd"
alias proj2="cd .."
alias proj3="cd devel/lib/tuginterface/"
alias exec="./tuginterface"
My current executable file after many trials is:
#!/bin/bash
alias proj="roscd"
alias proj2="cd .."
alias proj3="cd devel/lib/tuginterface/"
alias exec="./tuginterface"
But it still does not work.
The same post advises to create a script and after that an alias in the startup file.
Please advise on how to solve this problem and sorry if it is a simple question but I don't seem to catch the mistake I am making.
The script doesn't need to define aliases. Aliases are commands that you can type yourself. The script can just execute the commands directly.
#!/bin/bash
cd ~/catkin_docking_ws/devel/lib/tuginterface
./tuginterface
I've combined the three cd commands into one that jumps straight to the correct directory.
"But I thought cd doesn't work in shell scripts?"
It depends what you're looking for. When a script changes directory it affects later commands in the script, so in that respect it does work. The change of directory is only inside the script, though. The person calling the script won't see the directory change. Their current directory is unaffected.

Alias can't save forever on Mac?

I want to create my own alias to make some command more simpler.I add
alias ll='ls -l' in ~/.bashrc,like this:
ANDROID_NAME=/Users/smy/Library/Android/sdk
PYTHONPATH=/Library/Python/2.7/site-packages:$PYTHONPATH
PATH=$ANDROID_NAME/platform-tools:$PYTHONPATH:$PATH
export ANDROID_HOME
export PYTHONPATH
export PATH
#alias
alias ll='ls -l'
when I first add this alias to this file,I execute source command,like this:
source ~/.bashrc
then in this command window,it can works,but when I create a new command window,it can't recognize the ll alias,that is when I execute ll,such error exists:
-bash: ll: command not found
when I type source ~/.bashrc,it will work.
So my question is:
why the alias can't be recognized whenever I type it,why I must execute source command to make it work when new command window opened,and how I can resolve this. I'm working on mac,Anyone can teach me about this,thanks!
You need to use ~/.bash_profile or ~/.profile for login shells instead of ~/.bashrc. From the documentation:
When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
and:
When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.
When opening a new terminal window/tab, the shell should be opened as a login shell.
Insert the alias you need into ~/.bash_profile and ~/.bash
Go to Terminal -> Preferences -> Generals -> Shell opens with -> Command (complete path) -> /bin/bash
Only one things worked for me, and I haven't seen it mentioned yet.
In addition to
making sure that my .bash_profile file included the relevant alias/aliases
ensuring in the Terminal > Preferences > General window that the Shell opens with option selected is /bin/bash
I needed to ensure that the bash profile is loaded using bash -l each time a new window is opened. Probably spent 30m hunting for this, so hope it helps folks who are updating to the latest versions of their MacOS (like yours truly today)!

Defining bash aliases from Ruby script

I'm trying to set a Bash alias from a Ruby script. The intended functionality would be (from the Ruby script):
Open ~/.bash_aliases (or something)
Add alias line to file (e.g: alias foo="cd /bar/blah")
Source .bash_aliases
Exit Ruby script
Be able to use new alias
However, using the system command doesn't work because it launches a new subshell.
Any advice?
what you want to do is not doable.
the script you are launching cannot really alter the environment of the shell.
one way to do this would be to source the output of the ruby script and to have the script just generate the commands. This way you are instructing the shell to actually do the right thing.
something like
source $(my_ruby_script.rb)
have the script alter the aliases and at the end read and print out the file.

How can I create an alias to a directory so that I don't have to type the long path every time?

Currently, to get to the directory I need to type this:
cd /cygdrive/c/Users/NameOfUser/FolderBelongingToUser
Is there a way to make it so that I can just type something like:
cd FolderBelongingToUser ?
I'm familiar with z (uses ranking) and cdargs (uses shortcuts) but there are many other tools designed to make navigation in your shell easier and built-in solutions like CDPATH or the ** wildcard…
CDPATH
Adding something like this in your *rc file:
export CDPATH='.:~:/cygdrive/c/Users/NameOfUser/'
allows you to do exactly what you are after:
$ cd FolderBelongingToUser
or, better:
$ cd Fold<Tab>
**
If your bash is recent enough, you can do something like this:
$ cd **/foo
If you're using OSX you can open the hidden file named .bash_profile in your root user directory and add an entry like this:
alias define_your_shortcut='define your path'
You can do this for anything. For example here is an alias for your example:
alias FolderBelongingToUser='cd /cygdrive/c/Users/NameOfUser/FolderBelongingToUser'
Here's another example using a command to toggle hidden files
alias showfiles='defaults write com.apple.finder ShowAllFiles TRUE'
alias hidefiles='defaults write com.apple.finder ShowAllFiles FALSE'
After you make any changes to your bash_profile you'll need to either logout and login or you can open terminal and tell it to reload your bash_profile with this command
source ~/.bash_profile
I'm not personally familiar with Windows but if your using Windows a quick search result explained that this is how you would create a command prompt alias in Windows
AddConsoleAlias( TEXT("test"),
TEXT("cd \\<a_very_long_path>\\test"),
TEXT("cmd.exe"));
Alternatively it looks like someone provided a good answer to doing this in Windows here: https://superuser.com/questions/560519/how-to-set-an-alias-in-windows-command-line
Vim normally supports tab completion, so you can probably type something like
cd ~NamTabFolTab
which will expand to
cd /cygdrive/c/Users/NameOfUser/FolderBelongingToUser
Of course, if NameOfUser is you, then you can probably just type
cd ~/FolTab
You can add the following line to your .vimrc
cabbr FolderBelongingToUser /cygdrive/c/Users/NameOfUser/FolderBelongingToUser
Then you can can
cd FolderBelongingToUser
If you want to add more to the path (eg to specify a filename with :w) you can press / after FolderBelongingToUser and it will replace it with the full path and allow you to continue typing.
:ca[bbrev] is a Command line only abbreviation. See: :help :cabbr
If this is a permanent alias, then in your ~/.bashrc, create an alias:
alias FTBU='/cygdrive/c/Users/NameOfUser/FolderBelongingToUser'
You will then be able to access it in your shell by:
$ cd FTBU
As another trick, if you are only going to use the alias to change to the directory then simply add cd to the alias
alias FTBU='cd /cygdrive/c/Users/NameOfUser/FolderBelongingToUser'
You then need only type:
$ FTBU
to change to the /cygdrive/c/Users/NameOfUser/FolderBelongingToUser directory. However, if you plan to use the alias for any other purpose, leave the cd out of the alias definition.
If this is a temporary alias, you can simply create an alias from the command line with:
$ alias FTBU='/cygdrive/c/Users/NameOfUser/FolderBelongingToUser'
with the same results. (substitute anything you like for FTBU) Note: you remove an alias with the unalias command. Also Note: you should to check whether an existing system command exists with the name of your alias before assigning it. Simply type the proposed alias at the command line. If you receive something line bash: your_alias: Command Not Found, then you are good to go. There is no minimum number of characters required in an alias. So it you want to use a single-character, that's fine.

How to load .bash_aliases from perl/bash script

Lets say I have a perl script that setups a bash_aliases as follow
source file: bash_aliases.sh
alias w="cd /var/www"
install.pl
#!/usr/bin/perl
my $aliase_src= "${installation_dir}/bash_aliases.sh";
my $aliase_target="$ENV{'HOME'}/.bash_aliases";
if (! copy($aliase_src,$aliase_target))
{
print "Failed to copy file $aliase_src to Destination:$aliase_target. Reason:$! .";
die;
}
[command required to load .bash_aliases file]
I should be able to go as follow:
./install.pl
w
I dont want to :
./install.pl
source ~/.bash_aliases
w
I am unable to find command to load these aliases such that once the file is moved to home directory these aliases should be available to current script (install.pl) as well as all other script without running another command from command line e.g source ~/.bash_aliases OR . .bash_aliases
Edit
auto_load_aliases.sh using PROMPT_COMMAND (trying to following l0b0, answer)
#!/bin/bash
echo 'alias test="echo tested"' > $HOME/.bash_aliases
export PROMPT_COMMAND="$HOME/.bash_aliases";
this also did not work :
./auto_load_aliases.sh
test
What I expected what that auto_load_aliases.sh script sets the PROMPT_COMMAND , once it's set I have test aliase available.But that did not work.
~/.bash_aliases is not loaded by Bash by default, it is just a common convenience to avoid putting too much stuff in ~/.bashrc. If you want the aliases to be available in new Bash shells you have to source ~/.bash_aliases somewhere in ~/.bashrc. Some popular distributions (I believe Ubuntu is one) does this by default.
If you want to automatically source ~/.bash_aliases after changing it, you could try adding this in ~/.bashrc to source it after each command:
PROMPT_COMMAND='source ~/.bash_aliases'
If your alias file is very complex, you could make this faster by using inotifywait and only reloading the file when it changes.
Another way of loading aliases after changing them would be to replace the current shell with another one:
exec "$SHELL"
Bottom line, something has to change to automatically load new aliases. There is no built-in runtime configuration option in Bash to do this (and even then you'd have to enable that option).

Resources