bash - supplying variable as a file path - bash

I'm trying to specify a variable for opening up a file with a particular app, but no matter how I attempt to reference it, it's not working.
sublime1=/Applications/Sublime\ Text.app/
sublime2="/Applications/Sublime\ Text.app/"
sublime3="/Applications/Sublime Text.app/"
I've been trying different ways of setting the variable, but for each of the variations I've tried, it fails.
open ~/.zshrc -a $sublime1
open ~/.zshrc -a $sublime2
open ~/.zshrc -a $sublime3
The file /Users/matthew/Text.app does not exist
It gives me the same error for each, so I assume they're equivalent. Even when I try cd $sublime it also fails, but slightly differently...
bash: cd: /Applications/Sublime: No such file or directory
Update:
It was suggested by Charles to use a function to accomplish the task of quickly opening something in sublime.
sublime() { open "$#" -a "/Applications/Sublime Text.app/"; }
Will allow you to simply run
sublime ~/.zshrc

These assignments are correct:
sublime1=/Applications/Sublime\ Text.app/
sublime3="/Applications/Sublime Text.app/"
The problem is with the invocation. Variables used as command line arguments are subject to word splitting and globbing. You need to double-quote them, like this:
open ~/.zshrc -a "$sublime1"
open ~/.zshrc -a "$sublime3"

try using sublime1=$(/Applications/Sublime/Text.app)
and using chmod 770 Text.app on Text.app in the command line
sorry for my english...

Related

Open shell variable (assigned with path) using vim editor

I am assigning a shell variable say TEST1 in .bashrc like below:
TEST1='x/y'
In my Present directory (pwd), i have folders in following hierarchy:
pwd/x/y/ztext.c
I am at pwd. I want to open ztext.c file from bash shell by executing
vim $TEST1/ and Tab key
I expected shell will list ztext.c in screen. But it didnt. Since i know the file name, i entered like below:
vim $TEST1/ztext.c
vim was opening new file and not the existing file.
But cd $TEST1 is moving to x/y folder path. Problem is only when executed with vim like mentioned above
Please correct me what am i doing wrong here.
Note : I tried export and set keyword for the TEST1 variable. It didnt help. Observation is same.
I don't why it's not working - I just did it on my machine. Below I show
exactly what I'm seeing in my terminal:
mattb#flat:~/mytest
$ l -R
.:
x/
./x:
y/
./x/y:
ztest.c
mattb#flat:~/mytest
$ export TEST1='x/y'
mattb#flat:~/mytest
$ vim $TEST1/
Pressing Tab expands to:
vim x/y/ztest.c

MacOS .bash_profile - /usr/bin/alias: line 4: builtin: alias: not a shell builtin - how to create an alias?

I have no idea why it isn't working. Using MacOS and bash:
$ echo $SHELL
/bin/bash
Here is the content of the file ```.bash_profile``:
I would imagine that I'm using right file - How do I create a Bash alias? - and the right syntax...
UPDATE:
Tried also .bashrc
When I tried to reload it:
$ source ~/.bashrc
/usr/bin/alias: line 4: builtin: alias: not a shell builtin
UPDATE:
Yes, it looks weird
$ type alias
alias is a shell builtin
$ source ~/.bashrc
/usr/bin/alias: line 4: builtin: alias: not a shell builtin
Given the set of possibilities we've eliminated --
Since you're using bash 3.2, set -x may not be visibly rendering all hidden characters, such as a byte order marker at the beginning of your file.
If the BOM is in fact the problem, then the first line of the file will always cause an error regardless of its contents, and if you move the alias command to the second line, it will then function. Similarly, if you install a more recent version of bash from MacPorts or Homebrew, then running bash -x -l -i will show the hidden characters at the beginning of the command it's trying to run in the generated log.
To solve this, recreate the file in a text editor that doesn't save a BOM, or use "Save As..." and choose a format with no BOM in an editor that gives you the option.
Run the alias command to display all created aliases. Also, you should use ~/.bashrc for aliases, not .bash_profile.
echo "alias p='python -m SimpleHTTPServer'" >> ~/.bashrc
source ~/.bashrc

How to edit corrupted bash profile

I exported a PATH that is incorrect in my bash profile, and I can no longer open it to edit it. Even worse, my terminal is basically completely broken because of this.
If I run vim ~/.bash_profile
I get the following errors:
-bash: vim: command not found
-bash: sed: command not found
If I try to use a command like ls I get:
-bash: ls: command not found
-bash: sed: command not found
How can I fix my bash profile if I can't even edit it?
Your terminal isn't broken, bash is just lost because it is using this broken PATH variable, which you have saved in ~/.bash_profile. So, when you reload (source) your configuration, bash is simply re-reading from the same broken PATH.
To fix it, you must either edit the configuration or replace the file.
In the meantime, you can restore (what is likely) your default PATH temporarily, for the current shell session, from the command-line: PATH="/bin:/sbin:/usr/local/bin:/usr/bin:/usr/sbin:"
Otherwise, you must include the full path to each command you enter (as commented above) since bash no longer knows which directories to look in for these programs (commands).
Try /usr/bin/vim ~/.bash_profile to open the file for editing.
If you'd like to instead remove the file, try: /bin/rm ~/.bash_profile But don't forget to replace it!
Once you've successfully, edited or replaced the file, you need to source it for it to be loaded with each new instance of bash: . ~/.bash_profile.
Also, it is better to place your configuration in the ~/.bashrc file, though this would not have prevented the same situation from happening.

Can I open a file with geany from the terminal

I know there is a shortcut to open a file with sublime text from the terminal but id there something similar with geany, the only info I can find online about it is opening a file from geany to the terminal.
If Geany is inside your path, just call geany <file>. This should work on every platform, Geany is running.
Out of Geany's man page:
SYNOPSIS
geany [option] [+number] [files ...]
This will open files either in a new session or, if already a session of Geany is running, inside the existing one.
A file can be given as space separated list and might take even line numbers:
files ...
A space-separated list of filenames. Absolute and relative
filenames can be used. Geany also recognises line and column
information when appended to the filename with colons, e.g.
"geany foo.bar:10:5" will open the file foo.bar and place the
cursor in line 10 at column 5.
You can use open:
open [PATH_TO_FILE] -a [PATH_TO_GEANY_APP]
The a option defines which application to use.
To make this shorter, create an alias:
Something like echo 'alias geany="open $1 -a [PATH_TO_GEANY_APP]"' >> ~/.bash_profile && chmod u+x ~/.bash_profile. Then open a new Terminal window and type geany [path_to_file].

ZSH not recognizing my aliases?

Using iTerm2 with zsh and it isn't recognizing my aliases. Sometimes I have to work in an IDE and can't just easily vim something and the stupid people thought it a good idea to name their applications like MyReallyLongApplicationName.app and since .html files open by default in browsers, I have to:
open -a MyReallyLongApplicationName.app something.html
I have an alias in my .zshrc like:
alias ide="open -a MyReallyLongApplicationName.app"
But zsh won't recognize my aliases. I tried another one just to see if it was me but none of the aliases I create are recognized. Just get "zsh: command not found: ide" or whatever.
So I'm not sure what I'm doing wrong and I've been searching around all day trying to fix things in zsh and the like. As a note, I'm not a pro at Linux/Unix systems so if you're too technical and vague I probably won't understand what you're telling me.
Thanks!
if you do a very simple alias in zsh, does it work? open your .zshrc file, and add the following line:
alias ls='ls -GpF'
after adding that line, type this line in your Terminal:
source ~/.zshrc
tell us what happens. Also, just for shiggles, make sure you are using single quotes vs. double quotes, I have seen that make a difference in the past on different versions of shells/OS/whatnot.
Add "source ~/.bash_profile" to your ~/.zsh config file.
Put this line:
/source: 'source ~/.bash_profile' into ~/.zshrc
After saving changes in ~/.zshrc file, open a new shell window and execute the command in it.
Sometimes the simple solution is what we need...
Add "source ~/.bash_profile" to your ~/.zshrc config file
echo source ~/.bash_profile >> ~/.zshrc
I needed to manually add the alias to my zsh config file and then run the source command on it.
echo alias this='some command' >> ~/.zshrc
source ~/.zshrc
In my case issue was space b/w aliasName and equalTo. you should have to remove those space.
bad assignment
alias keu = 'k exec -it utils bash'
correct one
alias keu='k exec -it utils bash'
What I did, in this case, was created a separate file to store all my aliases. I found this way to be cleaner and easier maintained.
My aliases file is simply called aliases and within my .zshrc I have the following:
# Linking my aliases file
source ~/foldername/aliases
Make sure the double quotes are actual double quotes and not some other character which looks like double quotes.
I was editing ~/.zsh-aliases in OSX - TextEdit, which, when hitting the double quotes key substituted it for another special double quotes character, which is not what ZSH expects.
After editing the alias file with Sublime and replacing the old double quotes with actual double quotes everything runs just fine.
Hope this helps.
I had all my aliases on ~/.bash_profile, so i added at the last line on ~/.zshrc the following line: . ~/.bash_profile and it worked for me.
You should put alias at the end of ~/.zshrc file.
you can use below command to do that:
echo alias this='some command' >> ~/.zshrc
after that run
source ~/.zshrc
then, open a new terminal and execute the command in it.
I'm using both bash and zsh with one .bashrc, .bash_aliases and .zshrc file.
Put this in you .zshrc to load bash files:
# shortcut to refresh .zshrc
alias refz="source ~/.zshrc"
# Load bash files to zsh
test -f $HOME/.bashrc && . $HOME/.bashrc
test -f $HOME/.bash_aliases && . $HOME/.bash_aliases
If you have many bash aliases and functions you may will have some error messages like:
/proc/self/fd/13:12310: bad option: -t
caused by bash specific lines in.bash_aliases or .bashrc files
You can skip those problematic ones using:
if [ -n "$BASH" ] ;then
lines to ignore by zsh
fi
For example kubectl autocompletion
# To fix error massage .bashrc:16: command not found: shopt
# Check if bash is the current shell, if not, skip it
if [ -n "$BASH" ] ;then
# kubectl and bash completions
if [ -x "$(command -v kubectl)" ]; then
source <(kubectl completion bash)
complete -F __start_kubectl k
fi
if ! shopt -oq posix; then
if [ -f /etc/profile.d/bash_completion.sh ]; then
. /etc/profile.d/bash_completion.sh
fi
fi
fi
# Instead I need to put this line somewhere in my zshrc
# to have kubectl autocompletion replacing the skipped bash one:
plugins=(git git-flow brew history node npm kubectl)
# To fix error message .bash_aliases:4: parse error: condition expected: =
# Change these to this syntax to be used by zhs
# Not compatible with zsh:
if [ $HOSTNAME = "x1" ]; then
# Compatible with bash and zsh:
if [[ $HOSTNAME == "x1" ]]; then
I had a bad alias line that should have been obvious.
There is no error checking in these zsh custom scripts, so you may, like me, waste a couple of precious hours trying to find out why your custom aliases or functions are not loading in iTerm2.
I am using MacOS Ventura 13.1 and iTerm 2 v3.4, zsh 5.8.1.
I reloaded and using . ~/zshrc and followed a lot of the suggestions above.
I finally copied my list of aliases and function to the ~/.zshrc file.
Another ~/.zshrc gave me a bad pattern error.
The fix for my case was as follows.
In line with a Unicode string or something that is escaped and requires ' ' (single quotes), you must use the other quote character (double quotes) to encapsulate the alias sting.
In my case, I had entered.
alias fixitemerrow='printf '\e[?2004l''
The fix for this is:-
alias fixitemerrow=" printf '\e[?2004l' "
Do not add spaces in your alias assignment. This is here just for illustration.
Spaces would also require double encapsulation " ' ' ".
Need to create a profile for .zshrc and register the alias into it. (only if the profile isn't available)
cd ~
touch .zshrc && open .zshrc
add all the alias in .zshrc file
source ~/.zshrc
close and re-open terminal and run the alias.

Resources