Split a string in shell scripting explaination - string

I want to extract the directory. I have used the below mentioned shell script.
line='create word=/some/directory/name'
dir=${${${line##*=}#"'"}%"'"}
Though, it is working for me, I want to know the working of the 2nd line.
How exactly string split occur in the shell scripting.

These constructs are known as "Parameter Substitutions".
For example (directly from the Bash documentation):
${var##Pattern} Remove from $var the longest part of $Pattern that matches the front end of $var.
Parameter Substitutions in Bash
Parameter Substitutions in Zsh

Related

How to add characters to a string between specific characters in bash?

Right now I'm working on creating a script in linux bash shell that adds the word "-BACKUP" to a file name between certain points. For example, if I had a file/string called file1.txt I would want to add the "-BACKUP" between "file1" and ".txt" to make "file1-BACKUP.txt". How would I go about doing that? Would I use the basename command anywhere? In this situation, the extension and stem could be anything, not just what I gave as an example. All help is appreciated!
Use the substring processing parameter expansion operator % to remove the suffix from the string, then append the new text. The variable must be enclosed in braces for substring processing parameter expansion to work.
var="file1.txt"
echo "${var%.txt}.BACKUP.txt"

What does ./${SCRIPT##*/} mean in a bash script

I'm looking at a script that someone else wrote. There are several variables set including another script which is executed by way of variable interpolation as ./${SCRIPT##*/}. I know that normally simply using ${SCRIPT} accomplishes this. What is the ##*/ after SCRIPT for?
That is parameter substitution.
${var##Pattern} Remove from $var the longest part of $Pattern that
matches the front end of $var.
In this particular example, it is greedily removing the pattern */, which will remove all parts of the string stored in SCRIPT before and including the last /.
Consider the following example.
SCRIPT=/tmp/hiefe.txt
echo ./${SCRIPT##*/}
Output:
./hiefe.txt
From the documentation:
${var##Pattern} Remove from $var the longest part of $Pattern that matches the front end of $var.
So yeah, it basically returns the remainder of the path after the final /, contacted with ./. Example:
>> SCRIPT=/test/set/of/path.text;echo ./${SCRIPT##*/}
./path.text

How to echo a string with any content in bash?

I'm having an extremely hard time figuring out how to echo this:
![alt text](https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png "Logo Title Text 1")
I keep getting this error:
bash: ![alt: event not found
Using double quotes around it does not work. The using single quotes around it does work, however, I also need to echo strings that have single quotes within them. I wouldn't be able to wrap the string with single quotes then.
Is there a way to echo a string of ANY content?
Thanks.
EDIT: Here is some context. I am making a Markdown renderer that grabs the content of a code editor, then appends every line of the code individually into a text file. I am doing this by doing this:
echo TheLineOfMarkdown > textfile.txt
Unlike in many programing languages, '...' and "..." in Bash do not represent "strings" per se; they quote/escape whatever they contain, but they do not create boundaries that separate arguments. So, for example, these two commands are equivalent:
echo foobar
echo "fo"ob'ar'
So if you need to quote some of an argument with single-quotes, and a different part of the argument has to contain single-quotes — no problem.
For example:
echo '![alt text](https://... "What'"'"'s up, Doc?")'
Another option is to use \, which is similar to '...' except that it only quotes a single character. It can even be used inside double-quotes:
echo "\![alt text](https://... \"What's up, Doc?\")"
For more information, see §3.1.2 "Quoting" in the Bash Reference Manual.
! is annoying. My advice: Use \!.
! invokes history completion, which is also performed inside double-quotes. So you need to single-quote the exclamation mark, but as you say that conflicts with the need to not single-quote other single-quotes.
Remember that you can mix quotes:
$ echo '!'"'"'"'
!'"
(That's just one argument.) But in this case, the backslash is easier to type and quite possibly more readable.

What Does $* mean in shell scripting [duplicate]

What does $* mean in bash scripting?
I tried to search on google for it, but I found only about $0, $1 and so on.
From the man page:
* Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single
word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent
to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated
by spaces. If IFS is null, the parameters are joined without intervening separators.
So it is equivalent to all the positional parameters, with slightly different semantics depending on whether or not it is in quotes.
See this page:
http://tldp.org/LDP/abs/html/internalvariables.html#IFSEMPTY
The behavior of $* and $# when $IFS is empty depends
+ on which Bash or sh version being run.
It is therefore inadvisable to depend on this "feature" in a script.
It's all the arguments passed to the script, except split by word. You almost always want to use "$#" instead. And it's all in the bash(1) man page.
Its the list of arguments supplied on the command line to the script .$0 will be the script name.
It's a space separated string of all arguments. For example, if $1 is "hello" and $2 is "world", then $* is "hello world". (Unless $IFS is set; then it's an $IFS separated string.)
You can use symbolhound search engine to find codes that google will not look for.
For your query click here
If you see $ in prefix with anything , it means its a variable. The value of the variable is used.
Example:
count=100
echo $count
echo "Count Value = $count"
Output of the above script:
100
Count Value = 100
As an independent command it doesn't have any significance in bash scripting.
But, as per usage in commands, it's used to indicate common operation on files / folders with some common traits.
and with grep used to represent zero or more common traits in a command.

How to extract a string in csh?

I would like to extract a string as below:
setenv VARIABLE /opt/Application/File/Version22
I want to display only
File/Version22
on screen. It means /opt/Application/ is always the prefix. I want to display the rest of the string. How do I do it in csh?
Regards
Now that you've updated the question (thank you for that), it's clear that you always want to remove the /opt/Application prefix.
The most straightforward way to do that, which will work in any shell, is:
echo $VARIABLE | sed 's|^/opt/Application/||'
(It's usual to use / as a delimiter for replacements like this, but you can use any punctuation character; I'm using | to avoid conflicting with the / characters in the pattern.)
A more tcsh-specific, and possibly more efficient, way to do it is:
echo $VARIABLE:s|/opt/Application/||
It's likely to be more efficient because it's done within the shell and avoids invoking the external sed command. On the other hand, the overhead of executing sed is unlikely to be significant.
Note carefully that the :s syntax is not supported in the original csh. It is supported in tcsh and in some newer versions of csh. If you want to do this portably, just use sed.
C-Shell has the built-in string modifiers that can do this with very little code:
echo $VARIABLE:h:t/$VARIABLE:t
string modifiers:
:h = remove the last directory (aka head)
:t = remove every directory except the last one (aka tail)
I assume that you want to get the 2 last names in a filename:
setenv VARIABLE /opt/Application/File/Version22
set lastfile=`basename $VARIABLE`
set prevpath=`dirname $VARIABLE`
set previousfile=`basename $prevpath`
set file=$previousfile/$lastfile
echo $file

Resources