LinuxQuestions.org
Review your favorite Linux distribution.
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 03-28-2013, 09:39 PM   #1
Zero4
Member
 
Registered: Jun 2007
Posts: 63

Rep: Reputation: 0
bash variables


Hi
How can i modify a variable in a bash script.
for example
Code:
linux-001-345.txt
i want to change the var to
Quote:
linux-001.txt
Thanks for your help
 
Old 03-28-2013, 11:22 PM   #2
shivaa
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
Explain it little more, and let's know what exactly your script is. In the meantime you can try:
Code:
var=linux-001-345.txt
newvar=$(echo $var | sed -e 's/linux-001-345.txt/linux-001.txt/')

Last edited by shivaa; 03-29-2013 at 06:52 AM.
 
Old 03-29-2013, 05:54 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Or use just bash:
Code:
$ var=linux-001-345.txt
$ echo ${var%-*}${var##*[0-9]}
linux-001.txt
 
Old 03-29-2013, 08:46 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Another variation:
Code:
$ var=linux-001-345.txt
$ echo "${var/-[0-9][0-9][0-9]./.}"
linux-001.txt

These two links will give you all you need to know about the shell's built-in features for manipulating text in variables:

parameter substitution

string manipulation

Once a string is stored in a variable, it's almost foolish to launch a less-efficient external process to do something that the shell can handle on its own.

Last edited by David the H.; 03-29-2013 at 08:48 AM.
 
Old 03-29-2013, 08:09 PM   #5
Zero4
Member
 
Registered: Jun 2007
Posts: 63

Original Poster
Rep: Reputation: 0
Thank you, shivaa,grail and David the H
I am new to bash scripts. All three worked
Much appreciated
 
  


Reply


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
Bash variables mtm100 Linux - Newbie 5 10-31-2011 11:27 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 07:09 AM
bash variables pfaendtner Linux - Newbie 4 11-23-2004 02:00 PM
Variables in bash tcaptain Programming 1 03-03-2003 03:07 PM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration