LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-02-2017, 05:11 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
* and @ in substring extraction in bash


I understand how a substring extraction is done,
Code:
${string:position:length}
but I don't understand how I can actually use * or @ in this context. The explanation from Advanced Bash Scripting isn't enough:

If the $string parameter is "*" or "@", then this extracts a maximum of $length positional parameters, starting at $position.
Code:
echo ${*:2} # Echoes second and following positional parameters.
echo ${@:2} # Same as above.
echo ${*:2:3} # Echoes three positional parameters, starting at second.
So it extracts the maximum of $length position parameteres, but what from, if the string is * or @?

Can you offer any concrete examples?
 
Old 08-02-2017, 05:31 AM   #2
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
from the command-line arguments when it was invoked

Code:
test@ws1:~$ cat /tmp/example.sh 
#!/bin/bash

echo "${@:1:2}"

test@ws1:~$ /tmp/example.sh one two three
one two
 
Old 08-02-2017, 05:35 AM   #3
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Right, I simply ignored the word group "positional parameter". That was clearly unambiguous. Thanks!
 
Old 08-02-2017, 05:43 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Not sure I understand the question. By positional parameters the tldp is referencing command line arguments.
Code:
#!/bin/bash
echo $*
echo ${*:2:3}

./myscript.sh 1 2 3 4 5
1 2 3 4 5
2 3 4
A bit late...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] extraction of substring with delimiter in C kauuttt Programming 3 08-05-2015 10:17 AM
[SOLVED] substring in bash script j1alu Linux - Newbie 6 07-21-2010 11:23 PM
Bash - Substring Extraction and Substitution on same variable jax8 Programming 5 04-26-2009 06:20 AM
get a substring with a bash command xeon123 Linux - General 4 01-26-2007 03:50 AM
perl: substring extraction after specific char markus1982 Programming 2 10-06-2002 05:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration