How can I make a file named "\$*'PNP'*$\" using touch command - linux

How can I make a file named "\$*'PNP'*$\" using touch command?
what I did:
% touch '"\$*'PNP'*$\"'
% ls
"\$*PNP*$\"
I need ' beside P

One approach is to put the desired filename in a Try:
$ touch "\"\\\$*'PNP'*$\\\""
$ echo *PN*
"\$*'PNP'*$\"
If you run ls, be aware that it may put escapes in the file name:
$ ls
'"\$*'\''PNP'\''*$\"'
In the above touch command, escapes are required. First, to put a double-quote inside a double-quoted string, it must be escaped as \". Second to put a backslash in a double-quoted string, it must also be escaped as \\. Inside a double-quoted string, ' does not need to be escaped.
Alternative: single-quoted string
$ touch '"\$*'\''PNP'\''*$\"'
$ echo *PN*
"\$*'PNP'*$\"
The above has five strings:
'"\$*'
\'
'PNP'
\'
'*$\"'
Strings 1, 3, and 5 are single-quoted strings. Strings 2 and 4 are unquoted but escaped single-quotes.

Just wrap it into single quotes:
touch '"\$*'PNP'*$\"'

Related

YAML need to use # inside but not comment it

I need to use this code inside a YAML file to remove chars from a file, but on YAML # is comment and cannot execute all after the #
Example of what I want to execute: echo $(tr -d '[\[\]#,"]')
Is there any workaround to achieve want I need?
The easiest, probably, in you case, would be to use a folded block style, because your tr does have simple and double quotes in it, and going with a quoted string would need you to go in escaping characters, which could be a pain.
But for the sake of completeness, here are the possible ways:
foo: "# I am not a comment, as I am enclosed in double quoted string"
bar: '# me neither, as I am enclosed in a single quoted string'
baz: |
# so ain't I, in a literal style block
qux: >
# I don't differ from the three above, in a folded style block
So, in your case, what you could do is:
my_command: >-
echo $(tr -d '[\[\]#,"]')
This is just a matter of YAML syntax. You can enclose your command in single or double quotes and escape inner quotes using a backslash
(\)
For example, using single quotes and using \' to escape the literal inner single quotes:
script:
- 'echo $(tr -d \'[\[\[]#,"]\')'
Or the same with double quotes:
script:
- "echo $(tr -d '[\[\]#,\"]')"
You can also use the other string styles that don't require escaping as described in another answer.

Bash script can't cat file: No such file or directory, but the file does exist and CMD line cat does work on same path [duplicate]

Should or should I not wrap quotes around variables in a shell script?
For example, is the following correct:
xdg-open $URL
[ $? -eq 2 ]
or
xdg-open "$URL"
[ "$?" -eq "2" ]
And if so, why?
General rule: quote it if it can either be empty or contain spaces (or any whitespace really) or special characters (wildcards). Not quoting strings with spaces often leads to the shell breaking apart a single argument into many.
$? doesn't need quotes since it's a numeric value. Whether $URL needs it depends on what you allow in there and whether you still want an argument if it's empty.
I tend to always quote strings just out of habit since it's safer that way.
In short, quote everything where you do not require the shell to perform word splitting and wildcard expansion.
Single quotes protect the text between them verbatim. It is the proper tool when you need to ensure that the shell does not touch the string at all. Typically, it is the quoting mechanism of choice when you do not require variable interpolation.
$ echo 'Nothing \t in here $will change'
Nothing \t in here $will change
$ grep -F '#&$*!!' file /dev/null
file:I can't get this #&$*!! quoting right.
Double quotes are suitable when variable interpolation is required. With suitable adaptations, it is also a good workaround when you need single quotes in the string. (There is no straightforward way to escape a single quote between single quotes, because there is no escape mechanism inside single quotes -- if there was, they would not quote completely verbatim.)
$ echo "There is no place like '$HOME'"
There is no place like '/home/me'
No quotes are suitable when you specifically require the shell to perform word splitting and/or wildcard expansion.
Word splitting (aka token splitting);
$ words="foo bar baz"
$ for word in $words; do
> echo "$word"
> done
foo
bar
baz
By contrast:
$ for word in "$words"; do echo "$word"; done
foo bar baz
(The loop only runs once, over the single, quoted string.)
$ for word in '$words'; do echo "$word"; done
$words
(The loop only runs once, over the literal single-quoted string.)
Wildcard expansion:
$ pattern='file*.txt'
$ ls $pattern
file1.txt file_other.txt
By contrast:
$ ls "$pattern"
ls: cannot access file*.txt: No such file or directory
(There is no file named literally file*.txt.)
$ ls '$pattern'
ls: cannot access $pattern: No such file or directory
(There is no file named $pattern, either!)
In more concrete terms, anything containing a filename should usually be quoted (because filenames can contain whitespace and other shell metacharacters). Anything containing a URL should usually be quoted (because many URLs contain shell metacharacters like ? and &). Anything containing a regex should usually be quoted (ditto ditto). Anything containing significant whitespace other than single spaces between non-whitespace characters needs to be quoted (because otherwise, the shell will munge the whitespace into, effectively, single spaces, and trim any leading or trailing whitespace).
When you know that a variable can only contain a value which contains no shell metacharacters, quoting is optional. Thus, an unquoted $? is basically fine, because this variable can only ever contain a single number. However, "$?" is also correct, and recommended for general consistency and correctness (though this is my personal recommendation, not a widely recognized policy).
Values which are not variables basically follow the same rules, though you could then also escape any metacharacters instead of quoting them. For a common example, a URL with a & in it will be parsed by the shell as a background command unless the metacharacter is escaped or quoted:
$ wget http://example.com/q&uack
[1] wget http://example.com/q
-bash: uack: command not found
(Of course, this also happens if the URL is in an unquoted variable.) For a static string, single quotes make the most sense, although any form of quoting or escaping works here.
wget 'http://example.com/q&uack' # Single quotes preferred for a static string
wget "http://example.com/q&uack" # Double quotes work here, too (no $ or ` in the value)
wget http://example.com/q\&uack # Backslash escape
wget http://example.com/q'&'uack # Only the metacharacter really needs quoting
The last example also suggests another useful concept, which I like to call "seesaw quoting". If you need to mix single and double quotes, you can use them adjacent to each other. For example, the following quoted strings
'$HOME '
"isn't"
' where `<3'
"' is."
can be pasted together back to back, forming a single long string after tokenization and quote removal.
$ echo '$HOME '"isn't"' where `<3'"' is."
$HOME isn't where `<3' is.
This isn't awfully legible, but it's a common technique and thus good to know.
As an aside, scripts should usually not use ls for anything. To expand a wildcard, just ... use it.
$ printf '%s\n' $pattern # not ``ls -1 $pattern''
file1.txt
file_other.txt
$ for file in $pattern; do # definitely, definitely not ``for file in $(ls $pattern)''
> printf 'Found file: %s\n' "$file"
> done
Found file: file1.txt
Found file: file_other.txt
(The loop is completely superfluous in the latter example; printf specifically works fine with multiple arguments. stat too. But looping over a wildcard match is a common problem, and frequently done incorrectly.)
A variable containing a list of tokens to loop over or a wildcard to expand is less frequently seen, so we sometimes abbreviate to "quote everything unless you know precisely what you are doing".
Here is a three-point formula for quotes in general:
Double quotes
In contexts where we want to suppress word splitting and globbing. Also in contexts where we want the literal to be treated as a string, not a regex.
Single quotes
In string literals where we want to suppress interpolation and special treatment of backslashes. In other words, situations where using double quotes would be inappropriate.
No quotes
In contexts where we are absolutely sure that there are no word splitting or globbing issues or we do want word splitting and globbing.
Examples
Double quotes
literal strings with whitespace ("StackOverflow rocks!", "Steve's Apple")
variable expansions ("$var", "${arr[#]}")
command substitutions ("$(ls)", "`ls`")
globs where directory path or file name part includes spaces ("/my dir/"*)
to protect single quotes ("single'quote'delimited'string")
Bash parameter expansion ("${filename##*/}")
Single quotes
command names and arguments that have whitespace in them
literal strings that need interpolation to be suppressed ( 'Really costs $$!', 'just a backslash followed by a t: \t')
to protect double quotes ('The "crux"')
regex literals that need interpolation to be suppressed
use shell quoting for literals involving special characters ($'\n\t')
use shell quoting where we need to protect several single and double quotes ($'{"table": "users", "where": "first_name"=\'Steve\'}')
No quotes
around standard numeric variables ($$, $?, $# etc.)
in arithmetic contexts like ((count++)), "${arr[idx]}", "${string:start:length}"
inside [[ ]] expression which is free from word splitting and globbing issues (this is a matter of style and opinions can vary widely)
where we want word splitting (for word in $words)
where we want globbing (for txtfile in *.txt; do ...)
where we want ~ to be interpreted as $HOME (~/"some dir" but not "~/some dir")
See also:
Difference between single and double quotes in Bash
What are the special dollar sign shell variables?
Quotes and escaping - Bash Hackers' Wiki
When is double quoting necessary?
I generally use quoted like "$var" for safe, unless I am sure that $var does not contain space.
I do use $var as a simple way to join lines:
lines="`cat multi-lines-text-file.txt`"
echo "$lines" ## multiple lines
echo $lines ## all spaces (including newlines) are zapped
Whenever the https://www.shellcheck.net/ plugin for your editor tells you to.

I want to embed a single quote in a string

Usually, in order to embed a quotation mark in a string, \ (backslash) is used.
Does a backslash have a different meaning in a Bash script?
My code below isn't working: the quotation mark wasn't included, and the following errors were reported:
recursive_merge.sh: line 7: unexpected EOF while looking for matching `''
recursive_merge.sh: line 14: syntax error: unexpected end of file
I have no explanation. Line 14 doesn't even exist.
#!/bin/bash
#############this file should be in the directory directly upper than p0x. sphnum.txt should also be at the same directory
for i in 02 03 04 05 06 07 09 10 11 12 13 14 15 16 17 20 21 22 23 24 25; do
x=$(grep $i sphnum.txt |cut -c5-6)
y=$(grep $i sphnum.txt |cut -c8-9)
z=$(echo '\''$i'.ala.r'$x'.sph '$i'.ala.r'$y'.sph\'')
w=$(echo $i'.ala.r'$x'r'$y'.sph')
echo $z
echo $w
cd p$i/spheres.10_2_75/sph/
/project/Biogroup/Software/Docking/MergeSpheres.pl -s $z -o $w -n 500 &
cd ../../../
done
As tripleee points out in comments on the question, the best approach in this particular scenario is to use a double-quoted string, in which you can embed both variable references (e.g., $i) and single quotes as-is; e.g.: z="'$i.ala.r$x.sph $i .ala.r$y.sph'"
This answer focuses on the various approaches to producing / embedding literal ' chars. in strings, starting with the OP's misconception.
Your use of '\'' suggests that you're confused by the workaround that is commonly used to "embed" a single quote in an overall single-quoted string, which is not what your code does on the z=... line, because it starts with '\''.
If we simplify your command, we get:
echo '\''$i
which is a syntax error, because to Bash the single quotes are unbalanced, because '\' by itself is considered a complete single-quoted string containing literal \, followed by the opening ' of a second single-quoted string, which is never closed.
Again it's worth noting that "'$i" is the best solution to this specific problem: the ' can be embedded as-is, and including variable reference $i inside the double-quoted string protects its value from potentially unwanted word-splitting and filename expansion (globbing).
POSIX-like shells provide NO way to embed single quotes inside a single-quoted string - not even with escaping. Hence, the \ in '\' is simply treated as a literal (see below for a workaround).
The rest of this answer shows all approaches to producing a literal ', both inside and outside quoted strings.
To create a single quotation mark outside of a quoted string, simply use \':
$ echo I am 6\' tall.
I am 6' tall.
This quotes (escapes) the individual ' character only, using \.
But note that tokens placed outside the context of a single- or double-quoted string on a command line are subject to word-splitting and filename expansion (globbing).
To use a single quote inside a double-quoted string, use it as-is (no escaping needed):
$ echo "I am 6' tall."
I am 6' tall.
This is the best choice if you also want to embed variable references (e.g., $i) or commands (via command substitutions, $(...)) in your string (you can suppress interpolation by escaping $ as \$).
To use a single quote inside a single-quoted string (in which no interpolations (expansions) are performed by design), you must use a workaround:
$ echo 'I am 6'\'' tall.'
I am 6' tall.
The workaround is necessitated by single-quoted strings not supporting embedded single quotes at all; the '\'' part only makes sense "inside" a single-quoted string in that:
the leading ' terminates the single-quoted string so far
the \' then produces a ' literal individually escaped with \ outside the context of a quoted string.
the trailing ' then "restarts" the remainder of the single-quoted string.
In other words: While you cannot directly embed a single quote, you can break the single-quoted string into multiple pieces, insert individually \-escaped ' instances outside the single-quoted string as needed, and let Bash's string concatenation (which automatically joins directly adjacent string) piece it all back together to form a single string.
chepner points out in a comment that you can alternatively use a here-document with a quoted opening delimiter, which acts like a single-quoted string while allowing embedding of ' chars:
read -r var <<'EOF' # quoted delimiter -> like a '...' string, but ' can be embedded
I am 6' tall.
EOF
With an unquoted opening delimiter, the here-document acts like a double-quoted string, which, just like the latter, also allows embedding ', while also supporting expansions:
read -r var <<EOF # unquoted delimiter -> like a "..." string
$USER is 6' tall.
EOF
Finally, if remaining POSIX-compliant is not a must, you can use an ANSI C-quoted string string, which allows embedding single quotes with \';
note that such strings interpret control-character escape sequences such as \n, but otherwise, like a normal single-quoted string, do not perform interpolation of variable references or command substitutions:
$ echo $'I am 6\' tall.'
I am 6' tall.

Spaces and curly braces for variables in bash

Is there a way to make ${VAR} expand as if it were quoted in double quotes?
That's not what I wanted to see:
% A="some spaces in there"
% touch ${A}
% ls -1
in
some
spaces
there
Sure, I can use typical notation like "$VAR". But that's cumbersome when using quotes within quoted text, etc. I wonder if there's a way of expanding ${...} notation that would treat ${...} as if it were "${...}" while not using doublequotes themselves?
Not following accepted best practice (and learning how the shell really works) is very likely to bite you. Repeatedly. With zombie virus-infected fangs. Some things which can help:
You can use different quotes for different parts of a single parameter as long as the start and end quotes are next to each other:
$ printf '%q\n' "foo 'bar' baz"'nuu "boo" zoo'
foo\ \'bar\'\ baznuu\ \"boo\"\ zoo
You can set IFS in a subshell to avoid screwing up your entire script:
$ a="some spaces in there"
$ (IFS= && touch ${a})
$ ls -1
some spaces in there
You can set the IFS variable
to disregard spaces when the shell splits variables. This is also useful when taking in input that may contain spaces in loops.
$ cat /tmp/t.sh
IFS="$(printf '\n\t')"
A="some spaces in there"
touch ${A}
ls -l
$ /tmp/t.sh
some spaces in there
(If you have characters like * in your strings try a set -f to disable globbing (see help set) thanks #glenn jackman. But really, putting a * in a filename is asking for trouble!)
And the original:
$ cat /tmp/t.sh
#!/bin/bash
A="some spaces in there"
touch ${A}
ls -1
$ /tmp/t.sh
in
some
spaces
there
$

Replace forward slash with double backslash enclosed in double quotes

I'm desperately trying to replace a forward slash (/) with double backslash enclosed in double quotes ("\\")
but
a=`echo "$var" | sed 's/^\///' | sed 's/\//\"\\\\\"/g'`
does not work, and I have no idea why. It always replaces with just one backslash and not two
When / is part of a regular expression that you want to replace with the s (substitute) command of sed, you can use an other character instead of slash in the command's syntax, so you write, for example:
sed 's,/,\\\\,g'
above , was used instead of the usual slash to delimit two parameters of the s command: the regular expression describing part to be replaced and the string to be used as the replacement.
The above will replace every slash with two backslashes. A backslash is a special (quoting) character, so it must be quoted, here it's quoted with itself, that's why we need 4 backslashes to represent two backslashes.
$ echo /etc/passwd| sed 's,/,\\\\,g'
\\etc\\passwd
How about this?
a=${var//\//\\\\}
Demo in a shell:
$ var=a/b/c
$ a=${var//\//\\\\}
$ echo "$a"
a\\b\\c
Another way of doing it: tr '/' '\'
$ var=a/b/c
$ echo "$var"
a/b/c
$ tr '/' '\' <<< "$var"
a\b\c

Resources