LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 05-18-2010, 01:26 AM   #1
LadyAnne
LQ Newbie
 
Registered: May 2010
Posts: 3

Rep: Reputation: 0
Appending file extensions to filenames in bash scripts


Hi

Suppose I have a variable called filename which contains the name of a file but the name does not contain an extension.

How do I go about doing this?

Thanks
 
Old 05-18-2010, 01:40 AM   #2
ajeetsinghraina
Member
 
Registered: May 2010
Location: India
Distribution: Ubuntu 7.04/8.04/8.10/10.04, RHEL 6, OpenSUSE,Mandriva,Fedora, CentOS, XandrOS, Slackware, OpenSolar
Posts: 155

Rep: Reputation: 55
Can you elaborate?

Do you mean this:

Code:
filename = tinyscript
echo $filename
Code:
How do I go about doing this?
what confused you?
 
Old 05-18-2010, 02:06 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Is this what you are looking for: echo ${filename}.txt or fullname=${filename}.txt

filename is the variable that holds a value and .txt is a hardcoded extension. The braces make sure that filename recognized as such.

Hope this helps.
 
Old 05-18-2010, 02:24 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by druuna View Post
filename is the variable that holds a value and .txt is a hardcoded extension. The braces make sure that filename recognized as such.
In the two contexts shown, the braces are not wrong but are not necessary because the following "." is not a valid variable name character so terminates the variable name.
 
Old 05-18-2010, 02:32 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

@catkin: True.

I did hope it also shows how to append a hardcoded string to a variable. I.e.:

$ foo="cat"
$ foobar="${foo}kin"
$ echo $foobar
catkin
 
  


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] Rename filenames with corresponding filenames Power2All Linux - Software 4 12-02-2009 04:15 AM
Bash Script - adding file modification date to end of filenames in directory themonkman Programming 2 09-01-2009 11:45 PM
appending a file circuit_girl Programming 8 10-01-2006 11:00 AM
bash help, renaming file extensions trey85stang Linux - General 8 07-21-2005 04:51 PM
Appending Text Files From Bash Script alts Programming 3 11-18-2004 06:36 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration