~/.zshrc alias with a space - macos

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'"

Related

replace rm with gio trash [duplicate]

I am trying to use the solution of using sudo on my existing aliases as covered in many existing answers already and i am so confused as to why it is not working
alias sudo='sudo '
I keep all my aliases in .bash_aliases. Inside .bash_aliases I have this
function _test {
echo 'test!'
}
alias test='_test'
I reload the .bashrc each time; source .bashrc but when I run sudo test I always get
sudo: _test: command not found
The only strange thing that happens is that I get the following on reload and not on a new terminal
dircolors: /home/MYHOME/.dircolors: No such file or directory
but i feel this is a red herring.
As l0b0 says, aliases cannot be used in this way in bash.
However, you can pass a function through (and really, there's basically never a good reason to use an alias instead of sticking to functions alone).
_test() {
echo 'test!'
}
sudo_test() {
sudo bash -c "$(declare -f _test)"'; _test "$#"' sudo_test "$#"
}
...will define a command sudo_test that runs the function _test via sudo. (If your real-world version of this function calls other functions or requires access to shell variables, add those other functions to the declare -f command line, and/or add a declare -p inside the same command substitution to generate a textual description of variables your function needs).
To run an alias like alias_name it must be exactly the first word in the command, so sudo alias_name will never work. Ditto 'alias_name', \alias_name and other things which eventually expand to the alias name.

bash alias using sudo returning command not found

I am trying to use the solution of using sudo on my existing aliases as covered in many existing answers already and i am so confused as to why it is not working
alias sudo='sudo '
I keep all my aliases in .bash_aliases. Inside .bash_aliases I have this
function _test {
echo 'test!'
}
alias test='_test'
I reload the .bashrc each time; source .bashrc but when I run sudo test I always get
sudo: _test: command not found
The only strange thing that happens is that I get the following on reload and not on a new terminal
dircolors: /home/MYHOME/.dircolors: No such file or directory
but i feel this is a red herring.
As l0b0 says, aliases cannot be used in this way in bash.
However, you can pass a function through (and really, there's basically never a good reason to use an alias instead of sticking to functions alone).
_test() {
echo 'test!'
}
sudo_test() {
sudo bash -c "$(declare -f _test)"'; _test "$#"' sudo_test "$#"
}
...will define a command sudo_test that runs the function _test via sudo. (If your real-world version of this function calls other functions or requires access to shell variables, add those other functions to the declare -f command line, and/or add a declare -p inside the same command substitution to generate a textual description of variables your function needs).
To run an alias like alias_name it must be exactly the first word in the command, so sudo alias_name will never work. Ditto 'alias_name', \alias_name and other things which eventually expand to the alias name.

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

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.

make alias available in sub-shells - UNIX

How can I make all alias listed in
~/.alias
available in all sub-shells? I am new to unix so I don't know much about this.
Thanks
In your home directory there needs to be a .bashrc file You probably have one. For each alias add one line that looks like this:
alias[space]myalias[nospaceshere!]=[nospaceshere]'/path/to/command arg1 arg2 '
Example:
alias ll='ls -l '
You can add a trailing space if you feel one may prevent problems in the use of your alias.
In Ubuntu, you can write in ~/.bash_aliases:
. ~/.alias
Every time you open a new terminal, your aliases are defined.

Bash script to cd to directory with spaces in pathname

I'm using Bash on macOS X and I'd like to create a simple executable script file that would change to another directory when it's run. However, the path to that directory has spaces in it. How the heck do you do this? This is what I have...
Name of file: cdcode
File contents:
cd ~/My Code
Now granted, this isn't a long pathname, but my actual pathname is five directories deep and four of those directories have spaces in the path.
BTW, I've tried cd "~/My Code" and cd "~/My\ Code" and neither of these worked.
When you double-quote a path, you're stopping the tilde expansion. So there are a few ways to do this:
cd ~/"My Code"
cd ~/'My Code'
The tilde is not quoted here, so tilde expansion will still be run.
cd "$HOME/My Code"
You can expand environment variables inside double-quoted strings; this is basically what the tilde expansion is doing
cd ~/My\ Code
You can also escape special characters (such as space) with a backslash.
I found the solution below on this page:
x="test\ me"
eval cd $x
A combination of \ in a double-quoted text constant and an eval before cd makes it work like a charm!
After struggling with the same problem, I tried two different solutions that works:
1. Use double quotes ("") with your variables.
Easiest way just double quotes your variables as pointed in previous answer:
cd "$yourPathWithBlankSpace"
2. Make use of eval.
According to this answer Unix command to escape spaces you can strip blank space then make use of eval, like this:
yourPathEscaped=$(printf %q "$yourPathWithBlankSpace")
eval cd $yourPathEscaped
You can use any of:
cd ~/"My Code"
cd ~/M"y Code"
cd ~/My" Code"
You cannot use:
cd ~"/My Code"
The first works because the shell expands ~/ into $HOME/, and then tacks on My Code without the double quotes. The second fails because there isn't a user called '"' (double quote) for ~" to map to.
cd ~/My\ Code
seems to work for me... If dropping the quotes but keeping the slash doesn't work, can you post some sample code?
This will do it:
cd ~/My\ Code
I've had to use that to work with files stored in the iCloud Drive. You won't want to use double quotes (") as then it must be an absolute path. In other words, you can't combine double quotes with tilde (~).
By way of example I had to use this for a recent project:
cd ~/Library/Mobile\ Documents/com~apple~CloudDocs/Documents/Documents\ -\ My\ iMac/Project
I hope that helps.
A single backslash works for me:
ry4an#ry4an-mini:~$ mkdir "My Code"
ry4an#ry4an-mini:~$ vi todir.sh
ry4an#ry4an-mini:~$ . todir.sh
ry4an#ry4an-mini:My Code$ cat ../todir.sh
#!/bin/sh
cd ~/My\ Code
Are you sure the problem isn't that your shell script is changing directory in its subshell, but then you're back in the main shell (and original dir) when done? I avoided that by using . to run the script in the current shell, though most folks would just use an alias for this. The spaces could be a red herring.
When working under Linux the syntax below is right:
cd ~/My\ Code
However when you're executing your file, use the syntax below:
$ . cdcode
(just '.' and not './')
use double quotes
go ()
{
cd "$*"
}
The very simple way of doing this is-
$ cd My\ Folder
In bash, run DIR command and in the results you would see that the folder or path names having space between them has been written in the results like this -
$dir
My\ Folder
New\ Folder
Use single quotes, like:
myPath=~/'my dir'
cd $myPath
Avoid ~ in scripts; use $HOME instead.
I had a similar problem now were I was using a bash script to dump some data. I ended up creating a symbolic link in the script folder with out any spaces in it. I then pointed my script to the symbolic link and that works fine.
To create your link.
ln -s [TARGET DIRECTORY OR FILE] ./[SHORTCUT]
Mau or may not be of use.
I read all these, and they didn't seem to work on macOS Monterey. I then changed the header from #!/bin/sh to #!/bin/zshand that seemed to do the trick.

Resources