Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-05-2013, 10:04 AM   #1
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
Why use ${REPLY:0:1} and not $REPLY

What are the semicolon values for - is it to shift values?

echo -e "Do you wish to restore all these file(s)/directory(ies)? [Enter Y to restore all; N to choose single file]: \c"
        if [[ ${REPLY:0:1} = y ]] || [[ ${REPLY:0:1} = Y ]]; then
Old 03-05-2013, 10:08 AM   #2
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
To me, it's look like 'getopts' command line aguments. Can you post full script?
Old 03-05-2013, 10:23 AM   #3
Senior Member
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 4,984

Rep: Reputation: 1770Reputation: 1770Reputation: 1770Reputation: 1770Reputation: 1770Reputation: 1770Reputation: 1770Reputation: 1770Reputation: 1770Reputation: 1770Reputation: 1770
That's how you extract a substring from a string in BASH

All they're doing is extracting the first character from whatever the user entered and checking if it's a y or Y.
1 members found this post helpful.
Old 03-05-2013, 10:28 AM   #4
Registered: Aug 2002
Posts: 14,008

Rep: Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265
It is a substring function of the form ${string:position:length}. The first character position of a string is 0. Just some error checking.
Old 03-05-2013, 11:52 AM   #5
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955Reputation: 1955
BTW, the above could be made much cleaner. At the very least the two separate pattern matches can be combined into one: You can also fix it to match the first character directly instead of using the parameter substitution to extract it first.

if [[ ${REPLY:0:1} = [yY] ]]; then

if [[ $REPLY = [yY]* ]]; then

if [[ ${REPLY,,} = y* ]]; then
if [[ ${REPLY^^} = Y* ]]; then
But also, this kind of pattern match should usually be done with a case statement instead. It's nearly always faster, cleaner and more efficient than an if/test construct, and you can included as many conditions as you want.

case ${REPLY,,} in
    y*) echo "you selected yes" ;;
    n*) echo "you selected no"  ;;
     *) echo "what the heck is that?" ;;


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
webserver doesn't reply to external request but it reply's to local request ziba Linux - Server 4 05-11-2009 05:27 PM
Quick Reply "post reply" button. IsaacKuo LQ Suggestions & Feedback 9 12-04-2005 03:41 PM
please reply sukhbhela Solaris / OpenSolaris 1 09-19-2004 03:50 AM
post reply & submit reply buttons annehoog LQ Suggestions & Feedback 10 01-05-2004 06:43 PM
Evolution - reply - missing own reply emanuelgreisen Linux - Software 0 01-31-2003 04:40 AM

All times are GMT -5. The time now is 08:29 PM.

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