Bash script check if tilde '~' is an argument [duplicate] - bash

This question already has answers here:
Echoing a tilde to a file without expanding it in Bash
(3 answers)
Pass a special variable (~ tilde) to Java program
(1 answer)
Closed 5 years ago.
I have a simple bash script that can accept arguments that it will be treating as text strings, nothing more.
If I give it ~, without quotes, then the home directory /home/users/me is what's parsed. Quote it, "~", it's fine. The character "~" is what I want, not the home path.
Is there any way I can ensure an un-quoted ~ is treated exactly as the character "~", not the home directory alias?

The bash shell is expanding the ~ on the command line before the argument is passed to your script.
There might be a bash option that you can change in your shell, but that would affect everything in your shell, which doesn't sound like what you want.
The short answer I think is no. There's nothing you can do in your script to change how the parent shell expanded any arguments before passing them to your script.

Related

Cut off the path of the working directory in the Linux console [duplicate]

This question already has answers here:
How can I shortern my command line prompt's current directory?
(12 answers)
Closed 2 years ago.
When I am getting into my working directory, I have the next pathname in the console:
kravcneger#kravcneger-X751L:~/projects/gcc/my_project
That path is very long, and it increases the width of the terminal window.
How can I make the pathname shorter, so that I wouldn't have to expand the terminal for comfortable work?
A critical condition: to change the machine name and the working directory is prohibited. :)
Add (or change) in your ~/.bashrc file PS1 variable:
PS1='\h \W\$ '
Here, \h is the machine name, \W is the basename of the current directory, and \$ is the literal $.
SEE ALSO:
Controlling the Prompt (Bash Reference Manual)

How to add quoted parameters to a quoted command in a shell script? [duplicate]

This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Closed 2 years ago.
I want to compose a command in a shell script like this:
#!/bin/sh
APPLICATION="date"
PARAMETER="-d '2020-01-01 1:23'"
CMD="${APPLICATION} ${PARAMETER}"
${CMD}
The 'PARAMETER' is supposed to hold parameters that need to be quoted themself. Unfortunately it does not work like this. Escaping them via PARAMETER="-d \"2020-01-01 1:23\"" also does not work.
After you've build CMD up, it is just string. It contains what can be interpreted by you as a command, but the shell sees it as a bare string.
If you want the string to reinterpret it, you need to eval it:
eval "$CMD"
However, eval is often considered evil.

BASH brace expansion from a string variables [duplicate]

This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 5 years ago.
How do I expand a brace expansion that originally come from a string variables ? Note that the string variable is a requirement.
#!/usr/bin/env bash
TEXT_DIRS='opt/*/{doc,gtk-doc}'
My intention is reading a bash source from zsh, or maybe other language as well such as Perl or Python. Get the configuration from /etc/makepkg.conf, as below.
DOC_DIRS=(usr/{,local/}{,share/}{doc,gtk-doc} opt/*/{doc,gtk-doc})
It is all, for just, learning purpose.
Is that possible, to expand from string ?
The tricky thing here is that once Bash resolves the environment variable, it doesn't make another pass to process its contents again. You'd have to evaluate the content of the variable in another pass of the shell ( eg another shell command).
Here's one way to do that:
bash-4.4# TEXT_DIRS='/usr/*/{bin,src,lib}'
bash-4.4# bash -c ls\ $TEXT_DIRS
ls: /usr/*/src: No such file or directory
/usr/local/bin:
/usr/local/lib:
Here, I'm dynamically generating a shell command that I then evaluate to handle the 2nd expansion. (I took the liberty of changing the paths to something that would match on typical systems, so make sure to change it back if you try to test).
Dynamically generating code is always dangerous, if you can't trust the input. That's essentially how command injection attacks work. But use of eval in your own shell with trusted input is more or less "safe", though I rarely find myself using it unless in a contrived scenario like yours, or some of my own worse ideas.

In bash, what does dir=${0%/*} means? [duplicate]

This question already has answers here:
What does 'cd ${0%/*}' mean in bash?
(2 answers)
What is the meaning of ${0%/*} in a bash script?
(1 answer)
Closed 6 years ago.
I have found this piece of code while studying a bash script:
dir=${0%/*}
I suspect the code inside the braces to be regex but I don't see what it means. Any idea?
It is not a regex, but it is a pattern match. It sets dir to the name of the script, which is $0, but without the last slash and any non-slash after it, if there is a slash in $0. If there is no slash in $0, dir gets a copy of $0 unchanged. See "Parameter Expansion" in the Bash Hackers Wiki.

Tilde (~/) not working on if then statement in Shell script [duplicate]

This question already has answers here:
tilde expansion in environment variable
(2 answers)
Closed 8 years ago.
I have the following script
file=${file:-letsdump.sh}
checkfile="~/mysql_backup/$file"
if [ -e "$checkfile" ]; then
mv ~/mysql_backup/$file ~/mysql_backup/$file-bak-$(date +%d%m%Y_%H%M).bak
else
echo 'file doesnt exist'
fi
I know my file is there, I am thinking it could be something to do with the ~/ ?
It never finds the file
Double quotes (really, any quotes) prevent ~ from being expanded. Use instead:
checkfile=~/mysql_backup/"$file"
...or, even better, use $HOME in all scripts, and consider ~ to be a facility for interactive/human use only.
The ~ character is not expanded inside double-quoted strings.
In this context, ~ is equivalent to $HOME, which is expanded in double-quoted strings, so you could write:
checkfile="$HOME/mysql_backup/$file"
When followed by a user name, ~ refers to that user's home directory, which is not tracked by any environment variable; "~fred/" would not expand, but writing something that does expand correctly in that context is more difficult. You can leave it outside the quotes, but that could cause problems if the home directory path includes a space -- which I hope would never actually happen. Correction, thanks to Charles Duffy: String splitting is disabled on assignments, so that wouldn't be an issue.

Resources