Shell Expansion Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Expansion Question
# 1  
Old 08-22-2011
Debian Shell Expansion Question

hey all,

Code:
x="a1 b1 c2 d2"

echo ${x#*1} b1 c2 d2 
echo ${x##*1} c2 d2 
echo ${x%1*} a1 b

echo ${x%%1*}
a

what does one # stand for?
what does two ## stand for?
what does % mean?
what does two signs %% mean?
what 1* means here?
what *1 means here?

I searched google but I got hard explanation and hard english.
I need someone to explain each command to me in easy words!

thanks.
# 2  
Old 08-22-2011
Did you try the man page for ksh? (man ksh) The man page has the info that you are looking for. If you have additional questions, after reading the specific areas in the man page, one of us can help.
# 3  
Old 08-22-2011
The specific section you're interested in is "parameter expansion". You should find it documented in the man page of any shell that supports those idioms.

Regards,
Alister
# 4  
Old 08-22-2011
Quote:
Originally Posted by eawedat
hey all,

Code:
x="a1 b1 c2 d2"

echo ${x#*1} b1 c2 d2 
echo ${x##*1} c2 d2 
echo ${x%1*} a1 b

echo ${x%%1*}
a

what does one # stand for?
what does two ## stand for?
what does % mean?
what does two signs %% mean?
what 1* means here?
what *1 means here?

I searched google but I got hard explanation and hard english.
I need someone to explain each command to me in easy words!

thanks.
${X} is a variable in its full syntax

Quote:
what does one # stand for?
Mask a regular expression, taking the less greedy portion, starting from left to right.

e.i.
Match as less as possible, zero or more characters, followed by a literal '1'
In your case if x="a1 b1 c2 d2" it matches 'a','1' and stops, returning the remainder as the result. "b1 c2 d2"


Quote:
what does two ## stand for?
Mask a regular expression taking the most greedy portion starting from left to right
e.i
${x##*1} -> match as much as it can. Returns remainder.
if x="a1 b1 c2 d2" then it eats 'a','1',' ','b','1' and stops.

Quote:
what does % mean?
Start from right to left matching as less as possible.
Quote:
what does two signs %% mean?
Start from right to left matching as much as possible
Quote:
what 1* means here?
Match a literal '1' followed by zero or more characters. (translates to nothing or everything else after a '1' included)
Quote:
what *1 means here?
Match zero or more characters followed by a literal '1'. (Translates to everything before a literal '1' or nothing plus '1')

Click me. Some examples half way of the article.
This User Gave Thanks to Aia For This Post:
# 5  
Old 08-22-2011
Quote:
Originally Posted by eawedat
I need someone to explain each command to me in easy words!
Sorry, I'm not the one Smilie however you can take a look at http://wiki.bash-hackers.org/syntax/pe
These 3 Users Gave Thanks to danmero For This Post:
# 6  
Old 08-25-2011
thank you very much AiaSmilie
sorry for the late...
thanks danmero

---------- Post updated at 12:02 PM ---------- Previous update was at 10:37 AM ----------

Do Expansion work with csh? because I tried they work only with bash
# 7  
Old 08-25-2011
@eawedat: I don't know about csh. Most of it should also work in ksh.
This User Gave Thanks to g.pi For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use parameter expansion over a parameter expansion in bash.

Hello All, Could you please do help me here as I would like to perform parameter expansion in shell over a parameter expansion. Let's say I have following variable. path="/var/talend/nat/cdc" Now to get only nat I could do following. path1="${path%/*}" path1="${path1##*/}" Here... (8 Replies)
Discussion started by: RavinderSingh13
8 Replies

2. Shell Programming and Scripting

Bash expansion

Hello. I cannot write a command without using eval. Any help is welcome Note 1 : What does the function SOMETHING has no importance. Note 2 : What does the command find has no importance. It is an expansion variable problem : where to put or or or anythings else What works (FILTRE_1... (8 Replies)
Discussion started by: jcdole
8 Replies

3. Shell Programming and Scripting

Shell variable expansion in awk

I want to split one file input.tab into two separate ones, odd lines to input_reads1.txt, even lines to input_reads2.txt for a serial of files with similar name pattern. Also I want to "match" input/output file names to keep consistency of file name: CSEL_02.0_input.tab CSEL_03.4_input.tab... (2 Replies)
Discussion started by: yifangt
2 Replies

4. Shell Programming and Scripting

Bash shell expansion help

This is what I have in my directory. $ ls test1.txt test2.txt test3.txt test4.txt test5.txt test_script.sh This is what my shellscript looks like. #!/bin/bash for filename in /shell_expansion/*.txt; do for ((i=0; i<=3; i++)); do echo "$filename" ... (5 Replies)
Discussion started by: cokedude
5 Replies

5. Shell Programming and Scripting

Expansion does not work

Hi, I'm trying to figure out whether some files exist. Locations of those file are stored in a plain text file called temp.txt in this way: All environment variables ($LIB_HOME and $ORACLE_HOME) have been set using export command. Then I do: while read line; do ] && echo "OK ==> $line" ||... (6 Replies)
Discussion started by: AlbertGM
6 Replies

6. UNIX for Dummies Questions & Answers

Expansion within cp

I have a bunch of files which I need to transfer to another location... and some of these I need to skip. For e.g. let us say the files are: cust_abc.dat cust_xyz.dat cust_def.dat and I only want to move the first two. I want to do something like: cp cust_.dat <target> ... (1 Reply)
Discussion started by: jawsnnn
1 Replies

7. Shell Programming and Scripting

Need to print the expansion of the found string (the expansion is beween two delimiters '-' , '||'

Hi , could anyone help me out with this problem. sample.txt has this content : u001- this is used for project1 || u002- this is used for p2|| not to be printed u003- this is used for project3 || u004- this is used for p4 || u005- this is used for project5 || u006- this is used for p6... (9 Replies)
Discussion started by: Balaji PK
9 Replies

8. UNIX for Dummies Questions & Answers

sudo and expansion

Hi there, Can anyone explain me the following behavior? hfserver:~# ls -l /home/cronlogs/mysqldump* -rw-r--r-- 1 root root 10658464 2009-01-18 03:00 /home/cronlogs/mysqldump_20090118030002 -rw-r--r-- 1 root root 10651035 2009-01-19 03:00 /home/cronlogs/mysqldump_20090119030001 -rw-r--r-- 1... (4 Replies)
Discussion started by: chebarbudo
4 Replies

9. Shell Programming and Scripting

~ expansion in printf

Hi, I have a script that at one point prints to a file as follows: printf -- $2 > ~/.mydir/$1 The idea is to print to a hidden directory .mydir in my home directory. I've already sanitized the inputs and $1 is in the format path1/path2/filename and $2 is some user input. When I run this... (2 Replies)
Discussion started by: Rledley
2 Replies

10. UNIX for Dummies Questions & Answers

Linux Shell Question: how to print the shell script name ?

Suppose I have a script named "sc.sh" in the script how to print out its name "sc.sh"? (3 Replies)
Discussion started by: meili100
3 Replies
Login or Register to Ask a Question