Why can't I 'cd' into Bash aliases on Cygwin? - bash

I'm using Bash via the mintty terminal on Cygwin, and I've created two aliases in my .bashrc file in my Cygwin home directory.
alias croot="C:/cygwin64"
alias desktop="B:/Users/User/Desktop"
When I enter croot or desktop into the terminal, it seems to work fine:
B:/Users/User/Desktop: Is a directory
However, using those aliases with something like cd croot returns the error:
-bash: cd: croot: No such file or directory
What's going on here?

alias doesn’t work the way you think it does. Do this:
alias croot='cd C:/cygwin64'
croot
Or:
croot=C:/cygwin64
cd "$croot"
Result:
$ pwd
/

There is a way to make this work. But I would not recommend it. Use steven's answer instead.
$ help alias
alias: alias [-p] [name[=value] ... ]
Define or display aliases.
Without arguments, 'alias' prints the list of aliases in the reusable
form 'alias NAME=VALUE' on standard output.
Otherwise, an alias is defined for each NAME whose VALUE is given.
A trailing space in VALUE causes the next word to be checked for
alias substitution when the alias is expanded.
Options:
-p print all defined aliases in a reusable format
Exit Status:
alias returns true unless a NAME is supplied for which no alias has been
defined.
$ alias croot="C:/cygwin64"
$ alias desktop="B:/Users/User/Desktop"
$ alias cd='builtin cd ' # Notice the trailing space.
$ cd croot; pwd
/
Note that only the word immediately next to cd will be considered for alias expansion. Hence cd -P croot will not work.

Related

Uppercased command aliasing in bash script?

I have a script I'm trying to make at least somewhat platform-independent. Because I can't count on the PATHs or shell aliases of my users (some of whom have been known to create aliases matching "reserved words" meaningful to the shell), I've taken to aliasing all non-builtin shell commands with uppercase equivalents. This works fine when I do it semi-manually, e.g.
AWK=/usr/bin/awk
DATE=/bin/date
GREP=/bin/grep
, but not so well when I try it in a function by iterating through an array of commands:
createAliases() {
COMMANDS=(awk chmod date echo git mkdir)
WHICH=/usr/bin/which
for command in ${COMMANDS[#]}; do
${command^^}=$($WHICH ${command})
done
}
, which produces errors like AWK=/usr/bin/awk: No such file or directory. Is my attempt to use the string^^ up-casing mechanism interfering with the variable assignment? It looks like the shell is actually trying to run the (aliased) commands, which is not what I want.
Any assistance is appreciated!
The following seems to work:
shopt -s expand_aliases
createAliases() {
COMMANDS=(awk chmod date echo git mkdir)
WHICH=/usr/bin/which
for command in ${COMMANDS[#]}; do
alias ${command^^}=$($WHICH ${command})
done
}
Prefixing the assigment with alias actually registers the desired aliases.
The line shopt -s expand_aliases enables you to then use these alias from anywhere in the script, according to https://www.thegeekdiary.com/how-to-make-alias-command-work-in-bash-script-or-bashrc-file/

~/.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'"

Preventing 'process completed' after bashrc function in OS X

In my .bashrc file, I have the following lines:
alias cd='_cd'
function cd()
{
cd "$1"
PS1='[$USER] "$PWD" $ '
}
However, after sourcing my .bashrc, every time I try to run the command, I get a process completed message, and I am locked out of the shell.
[prompt] $ source ~/.bashrc
[prompt] $ cd ~
[Process completed]
How can I easily implement this function without getting the process completed message?
Your cd function is recursive, and eventually the shell gets too deep and gives up.
Ensure you're calling the shell's cd inside the function:
cd() {
builtin cd "$1"
PS1='[$USER] "$PWD" $ '
}
You don't have to do this if you define your prompt with: PS1='[\u] "\w" \$ ' -- see the PROMPTING section of your bash man page.
Declaring alias cd='_cd' does not mean you are changing the builtin command cd to _cd. It means you are making an alias of _cd that is invoked when you enter cd. Command expansion follows the order of aliases, functions, builtin and then executables in $PATH. So if there is an alias, function and builtin with the same name, the alias will be executed.
Next it seems you are trying to set your PS1 with a function, while as Jonathan explained it is better to just declare it plain in your .bashrc like
PS1='[$USER] "$PWD" $ '
I would recommend however to use the special characters the prompt recognizes instead of system variables.
$USER is the current user, which in PS1 can represented by \u
$PWD is the working directory, you have the option here to show the full path with \w or just the current with \W.
There are a lot of other useful options, but you should check them out by yourself.
https://www.gnu.org/software/bash/manual/bashref.html#Controlling-the-Prompt
So your prompt may be something like PS1=[\u] \w $

How to pass ags to a specific part of an alias

I made my own script to move "up" on unix (cd ..) and it is called like this: $ /path/to/script/myup args | cd where args may be nothing. I would like to alias this to "up" on my tcsh and bash shells so I can call it: $ up args. I thought it would be aliased like: alias up '/path/to/script/myup \!*| cd' because that is what I used for my cdls alias but its not working. How do I properly pass parameters to an alias?
perhaps you mean cd
`/path/to/script/myup\\!*`
This execute myup and passes its output to cd as a parameter. Note that they are backwards quotes

pwd alias causing infinite loop in UNIX (bash)

I have an alias, dir, which works fine using $(pwd) to show me the current directory I'm in:
alias dir='echo -e ${color1}jarvis: ${color2}you are currently in the ${color3}$(pwd)${color2} directory, sir.${NC}'
I would rather this alias be pwd, or have an identical one for pwd (so i can use dir or pwd to get the exact same response), but I seem to end up in an infinite loop everytime I try alias pwd=dir or
alias pwd='echo -e ${color1}jarvis: ${color2}you are currently in the ${color3}$(pwd)${color2} directory, sir.${NC}'
any ideas?
alias pwd='echo -e ${color1}jarvis: ${color2}you are currently in the ${color3}$(\pwd)${color2} directory, sir.${NC}'
Backslash in \pwd avoids the alias.
In bash, the environment variable PWD is always kept current. pwd is the shell built-in you shadowed with your alias, and /bin/pwd is always around in case it's 1984.

Resources