Tag Info

New answers tagged

0

To complement the other answers, zsh, fish and (t)csh are more helpful here in that they can help you show your mistake before it becomes a problem: If there's no *test.txt file in the current directory: zsh$ find . -name *test.txt zsh: no matches found: *test.txt fish> find . -name *test.txt fish: No matches for wildcard '*test.txt'. find . -name ...


2

You should always avoid mixing languages as much as possible. In this case you are trying to mix shell script into ruby script. Mixing languages convolutes your code and makes it fragile. The solution in this case is to use ruby's native environment variable support. $ GHREPO=ecx ruby -rjson -e 'j = JSON.parse($stdin.read); puts ...


0

It should be '"'$GHREPO'"' so the command is cat dummy.json | ruby -rjson -e 'j = JSON.parse($stdin.read); puts j["ChgSub"]['"'$GHREPO'"'][0]["Major"].to_s' @Etan was pretty close to the answer.


1

tl;dr version You are passing a string literal to the command/program, just like double-quotes but differ that single-quotes prevent variable and wildcard expansion while double-quotes expand them in to the string literal. Example: $ export MY_VAR=my_string $ echo "$MY_VAR" my_string $ echo '$MY_VAR' $MY_VAR The same applies to wildcards Hope it helps. ...


-4

In the first case, the argument *test.txt is considered an operator of the find command itself whereas with quotes, the argument *test.txt will be considered a parameter to a switch of find. If you have more than one text files with the .txt extension in your current directory, the following will fail as find will not see a *.txt argument: find . -name ...


20

The quotes protect the contents from shell wildcard expansion. Run that command (or even simpler just echo *test.txt in a directory with a footest.txt file and then one without any files that end in test.txt and you will see the difference. $ ls a b c d e $ echo *test.txt *test.txt $ touch footest.txt $ echo *test.txt footest.txt The same thing will ...


3

Variables don't get evaluated in a single quoted string. You need to use double quotes. If you can't or don't want to use double quotes for the whole string you can use them for just that part of the string. ruby -rjson -e 'j = JSON.parse($stdin.read); puts j["ChgSub"]["'"$GHREPO"'"][0]["Major"].to_s' That is three separate quoted strings next to one ...


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, ...


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 ...


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


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.


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


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 ...


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 is overhead to use sed just for one variable. In bash you can use ${varA/\#/\\\#}.


3

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


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. ...


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 ...


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 ...


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 ...


0

The first line works if you leave out the ^. Maybe it worked but it didn't work the way you assumed? I doubt that grep's behaviour has changed in such an important point. echo does not translate escape sequences by default. You need the -e for that. Similar with the shell. You need $'...' to make the shell use escape sequences.


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 ...


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"


0

The character & is special syntax in the shell, so what you've written runs the curl command in the background and runs the command page=i in the foreground. You need to put it inside the quotes. To refer to the value of a variable, put a $ before it. A variable substitution looks like this: $i — and it must be inside double quotes, not inside single ...


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 ...


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 @.


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 ...


0

I would like to add few more points here. The backtick (``) is actually called command substitution. The purpose of command substitution is to evaluate the command which is placed inside the backtick and provide its result as an argument to the actual command. The command substitution can be done in two ways one is using $() and the other is "``". Both ...



Top 50 recent answers are included