Hi
Can anyone explain me the following command.
This may be a minor one, but, please do explain in detail.
CHKTRANS=`echo ${0} | grep '/transforms/' | wc -l`
Nice answers from and — a few practical clarifications and safer alternatives that help the snippet behave predictably in real scripts.
Important nuances not yet covered: $0 is exactly the string the kernel/shell used to invoke the script — that can be an absolute path, a relative path, or just a basename if the script was found on $PATH. The literal pattern "/transforms/" requires a slash before and after the word, so it only matches when "transforms" appears as a path component (for example "./transforms/x" or "dir/transforms/x" or "/opt/transforms/x"), but it will not match if $0 is "transforms/x" with no preceding slash. Also remember that when a script is sourced (dot or source), $0 stays as the parent shell, so checks based on $0 will give unexpected results in that case.
Safer, clearer ways to test for a path component (avoid spawning wc, prefer shell builtins or grep -q, and avoid echo quirks):
# POSIX-portable pattern (no external tools)
case "$0" in
*/transforms/*) CHKTRANS=1 ;;
*) CHKTRANS=0 ;;
esac # Using grep but avoiding wc and echo
if printf '%s' "$0" | grep -q '/transforms/'; then
CHKTRANS=1
else
CHKTRANS=0
fi If you need to test the script's canonical location (resolve relative paths and symlinks) first compute a normalized path and then test it. A common portable approach is to cd to dirname and use pwd -P before applying the pattern match.
Final tips: always quote variables (use "$0"), prefer builtins (case or [[ ... ]] in bash) for speed and simplicity, and avoid relying on echo for arbitrary strings — use printf when you need to pass the value to another command. As pointed out, ${0} and $0 are equivalent here; braces are only necessary when characters follow the variable name.
Jump to Post— masijade 1,351It greps whether the command used contains the string "/transforms/" and returns 1 if it does and 0 if it doesn't.
$0 (or, iow, ${0}) refers to the actual command used on the command line.
Jump to Post— masijade 1,351No, that is just looking for that EXACT string in the provided argument (not in the contents of the file it references).
Jump to Post— sepp2k 378A variable can be referred to as either
$VARNAMEor${VARNAME}. The latter form is useful if the variable name is followed by something that's not supposed to be part of the variable name. For example say you have a variableFOOand you want to print the contents of …
It greps whether the command used contains the string "/transforms/" and returns 1 if it does and 0 if it doesn't.
$0 (or, iow, ${0}) refers to the actual command used on the command line.
$0 is used to refer file-name.
My question is "/transforms/" is any predefined command or a word "/transforms/" in my file..?
No, that is just looking for that EXACT string in the provided argument (not in the contents of the file it references).
ok thank you
In the same way can you tell me the difference between $0 and ${0} ?
A variable can be referred to as either $VARNAME or ${VARNAME}. The latter form is useful if the variable name is followed by something that's not supposed to be part of the variable name. For example say you have a variable FOO and you want to print the contents of the variable followed by the string "_BAR". If you write echo $FOO_BAR it will look for a variable named FOO_BAR. So you write echo ${FOO}_BAR instead.
In this case there's a space directly after the ${0} so the braces aren't actually necessary. So in this case there is no difference between the two.
On my system, the 'transforms' command doesn't exist, so the output of this command is always '0'. I looked in all of my application repositories, and could not find it - even trying to install what I thought might be relevant packages. Still no luck! Anyway, let's look at the processes in detail.
So, I have to ask, what EXACTLY are you trying to do?
The command 'echo ${0}' returns the command name that executed - in this case, 'bash' - the shell command that I am running.
Since the code was almost certainly part of a shell script, not typed directly into the shell, $0 would be the name (and path) of the script file, not the name of the shell.
That is piped into the command "grep '/transforms/'" - which due to the path specified, must exist in the / (root) directory of your system - unlikely...
What? grep 'transform' runs the executable grep with the argument /transform/. grep must be located somehwere in your $PATH (most likely /bin) and, unless you have a completely non-standard system, it will.
The output of that is piped to the 'wc -l' command which would return the number of lines from the '/transforms/' command. Since that (/transforms/) doesn't exist, the output is '0'.
It produces the number of lines in the output of the grep command. And as masijade already pointed out, grep will produce one line of output if $0 contained the substring /transform/ (which will be the case if the script is placed inside of a directory named transform or anywhere below (i.e. in a sub-directory, sub-sub-directorc etc. of) it. Otherwise it will produce no output.
Thanks, but i am not clear differrence between this two sum.
I'm afraid I do not understand your question. None of the code posted in this thread contains any sums.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.