Get current directory through alias in git-bash (windows) - shell

This command prints the directory name just fine:
echo ${PWD##*/}
This alias in .bashrc does not:
alias echodir="echo ${PWD##*/}"
They both work fine in the home directory, but after changing directories only typing it in manually works. The alias still prints the home folder. I understand this is because git bash works with nested shells or something - the base shell doesn't change directories at all, but the surface one does.
Is there a way to create an alias that works as expected?

I'm just posting the answer twalberg put in the comments(he gets the credit):
As a one-off command:
echo "-----Updating "${PWD##*/}"-----"
As an alias:
alias updating='echo "-----Updating "${PWD##*/}"-----"'

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.

How to create an alias for .. to mean cd .. in macOS shell

I often find myself typing cd.. (which results in -bash: cd“: command not found) in stead of cd ..
So would like to add an alias for this in my .bashrc or better yet, I would like an alias for .. to mean cd ..
I can't get it to work however; I tried several options:
alias ..=cd .. results in alias ..='cd'
alias ..='cd ..' results in alias ..=''cd'
Also escaping the dots in various ways doesn't work:
alias ..=cd \.\. results in alias ..='cd'
alias ..=cd '\.\.' results in alias ..='cd'
What is correct way to do this right?
This works for me on linux (Ubuntu 16.04) with bash v4.3.46. I believe it won't be any different for macOS.
alias '..'='cd ..'
I assume that you are interested in solutions working for bash.
If you want to solve this with an alias, you have to use a name which starts with a letter, for instance
alias up='cd ..'
Maybe more interesting for you would perhaps a general solution, where typing any directory name would cd to this directory. You can do it by placing into your .bashrc the command
shopt -s autocd
After this, .. would bring you one directory up, ~ would bring you to your home direcory and aunt/bertie would cd you right to your aunt.

Bash alias doesn't work in cygwin

Can someone please explain to me how to set up bash aliases? I am using cygwin on windows 8.
I added alias my_first_alias='git status' at the end of /.bashrc file. Typing my_first_alias into cygwin results in -bash: my_first_alias: command not found.
Trying to restart cygwin, running . .bashrc doesn't help with that.
The syntax of your alias command is correct and should be working as long as the alias command is actually being executed. It sounds like your .bashrc file is not being loaded when you start your bash shell. Make sure you have the following in your ~/.bash_profile file:
[[ -s ~/.bashrc ]] && source ~/.bashrc
Also make sure that the location of .bashrc and .bash_profile are in your home directory. Above you have referenced /.bashrc. I doubt "/" is your home directory. You can determine the location of your home directory from the shell by entering the command:
cd; pwd

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.

Creating aliases in .bash_profile that run a shell script

So I have a script called spotlyrics.sh that I want to be able to run using the command "lyrics" in the terminal.
I have opened up my .bash_profile and am wondering how I can create the alis which 1) finds the script and then 2) executes it
The file is inside a folder called bash at the following path
/Users/username/Documents/bash
What I have so far (inside my bash profile), which doesn't work because I guess it's not "executing" the script.
alias spotlyrics=“/Users/username/Documents/bash/spotlyrics.sh“
I get the following error when running "spotlyrics" in the terminal:
-bash: “/Users/username/Documents/bash/spotlyrics.sh“: No such file or directory
Would love some help, thanks!
You've been editing your .bash_profile with something that is not a proper text editor. The quotation marks are not ASCII, and therefore not actually quotation marks as far as the shell is concerned.
Instead of beating around the bush with aliasing a script to a name it mostly already has, why not put the script in a directory in PATH and let it be its own command?
mkdir ~/bin
echo 'PATH+=:$HOME/bin' >> ~/.bashrc
mv "/path/to/spotlyrics.sh" ~/bin/spotlyrics && chmod +x ~/bin/spotlyrics
Then restart the shell (log out and back in) and you won't need the alias.
Well, the shell scripts are not executable by just calling it's name, they should be run using "source" command(in case of not c-shell, dot command(.) can also be used).So while adding an alias in .bashrc or .bash_profile for running a shell script append source command before the path to the shell script.
In your case probably this should work:`
alias spotlyrics='source /Users/username/Documents/bash/spotlyrics.sh'`
Please let me know if it doesn't work. Because it worked for me.

Resources