$0 shell variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting $0 shell variables
# 1  
Old 09-25-2009
$0 shell variables

Would appreciate if someone can explain the ${0##*/} line. What does it do?
I am aware that $0 is the script name, $# is number of arguments passed in, $* is all the arguments. With the curly brackets {} added in, what's the eventual effect?
Does ${0##*/} actually equals $0$#$*? (something like factorising?)

myscript.sh
Code:
.
.
LOGFILE=$MYLOG/${0##*/}.logfile
.
.

# 2  
Old 09-25-2009
What does your code return ... I just test on my shell ... and I get

Code:
[~]$ cat try.sh
#!/bin/bash
MYLOG="mylog"
LOGFILE=$MYLOG/${0##*/}.logfile
echo $LOGFILE


Output:
Code:
[~]$ ./try.sh
mylog/try.sh.logfile

Do you get different output ??
# 3  
Old 09-25-2009
This is a parameter expansion implemented by ksh, bash and possibly other shells and documented in these shell respective manual pages which I recommend you to read.

It means here remove all directory components that might appear in $0. It is equivalent to using `basename $0` but faster.
# 4  
Old 09-25-2009
Quote:
Would appreciate if someone can explain the ${0##*/} line. What does it do?

that is shell's string operations.

yeah, if you see the result it is same as basename $0

${0##*/} is removing everything before "/" from the variable $0.

so its returning just the script name itself, without absolute path or ./ ( if executed from the current dir).

built in shell operations are always faster than external commands eg basename as per my knowledge.

see man pages for shell for more details.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

2. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. Shell Programming and Scripting

Parse variables from C++ to shell

Hi All, #include <iostream> int main() { std::int foo = 34; system("mkdir /home/linuxUser/fooDir"); system("vi fooFile") system("write foo in fooFile") foo = 43; foo = read foo from fooFile; std::cout << "foo = " << foo ; } result should be foo = 34 can... (3 Replies)
Discussion started by: linuxUser_
3 Replies

4. Shell Programming and Scripting

Two shell variables using nawk

How do you use two shell variables in awk? I am using Solaris 10 and don't have GNU products installed. File (transportation.txt) contents: car make boat model airplane landing snowmobile track bicycle helmet sled housing Thanks to this forum this code works (prints everything from the... (4 Replies)
Discussion started by: thibodc
4 Replies

5. HP-UX

Shell script variables

hi everyone, i'm writing shell script on hp-ux server that run by root user then (inside the script) su to database user and appl user..the reason for this script is to run some commands involve all users root and database and appl..anyway, variables when root in control is ok but when su, the... (1 Reply)
Discussion started by: neemoze
1 Replies

6. Shell Programming and Scripting

Shell variables problems

hi, i need some help, the situation is this 1-file of variable enviroments DIR1=/tmp DIR2=otherdir/mydir 2-file of list of files (all the names references whic variables of first point) ${DIR1}/${DIR2}/onefile Well now i create a shell script whic... (5 Replies)
Discussion started by: chipcmc
5 Replies

7. Shell Programming and Scripting

Awk, shell variables

Hello, I've been trying to figure out how to use variables inside the AWK command and use it back in the korn shell sript. in my script I have lots of awk commands like this grep Listen /etc/ssh/sshd_config | \ awk '{ if ($2 == "22" ) print "OK"; else print "not OK" }' ... (3 Replies)
Discussion started by: mirusko
3 Replies

8. Shell Programming and Scripting

variables in shell

hi, i'm new in shell scripting and i'm working on bash on solaris 5.9 after try many stuff with unexpected results, i wonder: it is not posible in bash, to use a variable that was created inside a loop, out of it? i mean, for instance: cat mytext | \ while read text do viko=$text... (2 Replies)
Discussion started by: viko
2 Replies

9. Shell Programming and Scripting

Shell Script Variables

HI guys I need to store the output of a sql query in a variable, can you tell me how to do that eg) select count(*) from s_escl_req $count = count(*) from s_escl_req how would i store the count(*) from the sql statement in a variable called $count. thanks (3 Replies)
Discussion started by: ragha81
3 Replies

10. UNIX for Dummies Questions & Answers

bash shell variables

Hi everyone, I have added this to my .bash_profile. Whenever I log in and when I type javac I get a error message (java: command not found). Does the order counts? PATH=$JAVA_HOME/bin:$PATH:$HOME/bin JAVA_HOME=$JAVA_HOME/usr/local/jdk1.3.1_02 export JAVA_HOME PATH Thanks ny (3 Replies)
Discussion started by: xNYx
3 Replies
Login or Register to Ask a Question