4

I want to create an alias that will print out the current working directory sub name.

I have this:

BASENAME=${PWD##*/}
alias wai="echo $BASENAME"

This outputs the directory subname of the directory .bashrc is stored in. I want it to be the current working directory.

Sorry if this is simple, I'm new to bash.

Nick Barone
  • 148
  • 3
  • 12
  • You could just use pwd and no alias. – ed. Oct 03 '11 at 16:37
  • I need to actually use the basename in another alias, not just print it. I'm using this alias (wai) to try and extract the basename before I go any further. – Nick Barone Oct 03 '11 at 16:38
  • **Dear Google searcher**: Check your quotes, a simple fix for me was to use a single quote `'` instead of double quotes `"` in the alias definition. See @kgong's answer below for more. – PatKilg Jun 27 '17 at 16:42

3 Answers3

10

The trick is really just in the single quotes. I believe you can achieve this by doing the following:

'echo ${PWD##*/}'

More details: Difference between single and double quotes in Bash

Community
  • 1
  • 1
kgong
  • 145
  • 1
  • 7
  • 1
    I think this is actually the answer I was looking for, as it led to the expected behavior based on the question. For reference, I use the following alias to launch the corresponding python venv matching that project from the project directory as follows `alias use-env='source ~/env/${PWD##*/}/bin/activate` – PatKilg Jun 27 '17 at 16:40
3

There is a command for this:

$ pwd
/path/to/current/directory

Am I missing something?

Edit based on your comment:

alias foo="pwd | rev | cut -d '/' -f1 | rev"
jman
  • 11,334
  • 5
  • 39
  • 61
1

What about

alias wai='basename $PWD'
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134