Tag Info

Hot answers tagged

23

In cd ~/z/ you are using Tilde expansion to expand ~ into your home directory. In BASE="~/z", you are not because you quoted the ~ character, so it is not expanded. That is why you get a message complaining about a nonexistent ~ directory. The solution is to not quote it, i.e. BASE=~/z in order to let the expansion occur.


9

@jw013 has given a valid explanation and solution. But there may be some cases when you do want to quote the paths, e.g. when they contain multiple spaces or special symbols. In this case you should use $HOME instead of ~, i.e. your BASE="~/z" will become BASE="$HOME/z" and work correctly, because parameter substitution is interpreted in double quotes, ...


6

When your variable is empty, your command becomes: [ -e ] In this case, you call [..] with one argument -e. String "-e" is not null, so test return true. This behavior is defined by POSIX test: In the following list, $1, $2, $3, and $4 represent the arguments presented to test: 0 arguments: Exit false (1). 1 argument: Exit true ...


5

[ introduces a character class and ] closes it. If sed sees only ] (i.e. the closing command), it will be OK, sed assumes this is not special command. Using only [ (without a closing ]) confuses sed, so it must be escaped. On a side note, there is no need for g (global) flag (only one substitution per line) escape [ in replacement. I managed your ...


4

The ' single quote character in your echo example gets it literal value (and loses its meaning) as it enclosed in double quotes ("). The enclosing characters are the double quotes. What you can do is print the single quotes separately: echo "'"'$a'"'" or escape the $: echo "'\$a'"


3

You misunderstand the documentation: having it's special meaning inside, would shield $ from the special interpretation "Having its special meaning" means that it is interpreted specially not literally. Single quotes prevent $ from being expanded. But single quotes within double quotes are literal characters i.e. they do not affect anything. If you ...


3

In Bourne-like shells (except zsh), leaving a variable expansion unquoted in list context is the split+glob operator. In: cond="-name '*.txt'"; echo $cond The content of $cond is first split according to the value of the $IFS special variable. By default, that's on ASCII space, tab and newline characters. So that's split into -name and '*.txt'. Then the ...


3

You missing quote in your sed expression. Try: $ varA="ASD# 1" $ echo "$(sed 's/#/\\#/g' <<< "${varA}")" ASD\# 1


3

ANSI-C Quoting According to the Bash manual, this is called ANSI-C quoting. The manual says: Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. In practice, this means that '\t' will not be expanded into a tab character, but $'\t' will. The ...


3

You must tell apart shell escaping and grep escapes. Basic regular expressions (BRE; which are used if grep is used without the option -E) treat (, ) and | as normal chars. The sequences \( and \| have special meaning. Without the quotes the shell treats the backslash as escape character and removes it i.e. grep doesn't see it. With quotes grep sees the ...


2

The problem is you need to quote the backticks in your alias definition. Double quotes (") do not quote command substitution. You will need single quotes ('). Use alias via='vim `ls -rt | tail -1`' Do not use alias via="vim `ls -rt | tail -1`" If you do that the command substitution happens when you define the alias, not when you run it. Try typing ...


2

To escape slashes, you can use any character other than a forward slash to separate regular expressions. E.g. (foo1/ -> foo2/): sed "s@foo1/@foo2/@" sed "s|foo1/|foo2/|" To escape brackets (this work also for slashes) you've to put a backslash character before the delimiting character causes the character to be treated literally. E.g. (foo[] = -> ;foo[] ...


2

The testcase can be fixed simply by changing the double quotes to single quotes, but you might like to know about GNU parallel. You can do the same thing like this: parallel echo {/} ::: /some/path/*dat (You may need to quote the curly braces, in some shells.) As well as having clearer syntax, parallel will run the multiple commands at once. Your ...


2

In case B: find . -iname *gall* The shell will expand *gall* into a list of all files matching that pattern. Since you only have one file in your current directory matching that pattern, this becomes: find . -iname gallifrey-road-doctors-14437-1366x768.jpg ...so find will search for files matching that exact name. ...


2

Get used to quoting variables. Always quote them; that prevents misassessments when it is necessary: if [ -e "$MYAPPPATH" ] This would not have happened with the bash-specific compound command: if [[ -e $MYAPPPATH ]]


1

There are some other ways: Check variable before (with editions according to comments) if [ -n "$MYAPPPATH" ] && [ -e "$MYAPPPATH" ] ; then ... Use locate* instead of find if locate -w "$APPDIR/myapp*" ; then ... *1 To use locate don't forget to update files database by updatedb


1

There is overhead to use sed just for one variable. In bash you can use ${varA/\#/\\\#}.


1

Archemar explained why this failed on [, here's another way of making this particular replacement: $ sed "s/^./['/;s/.$/']/" file ['AAA','ACMEDEMO2','ACMEDEMO3','ACMEDEMO4','RENTCOGH','TESTENT','DORASINE','LOKAWINK','BBB'] Or, using & which means "whatever you just matched": $ sed "s/^./&'/;s/.$/'&/" file ...


1

You should probably realize that there's no single type of regular expressions. There are at least basic regular expressions or BRE (sometimes only RE), extended regular expressions or ERE and perl compatible regular expressions or PCRE. All those languages use slightly different syntax. Current versions of GNU grep support all three and the BRE are ...


1

You are using strong quoting(single quoting) in the script as strong quoting prevents the interpretation of contents inside the single quote except the quote. Single quoting turns off the special meaning of $ in a script. #!/bin/bash echo "$PATH" Works! #!/bin/bash echo '$PATH' Not Works! Output remains same as input


1

You must use double quotes instead of single quote: date -d "14 Oct 1582 + $d days"


1

ls /some/path/*dat | xargs -n 1 -I @ sh -c 'echo `basename "@"`' The basename is executed too early in your code. If you are not sure that there are no spaces or tabs in the paths then you should use -d \\n (or find ... -print0 | xargs -0 ...) and mind the " around the @.



Only top voted, non community-wiki answers of a minimum length are eligible