Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-11-2009, 03:29 AM   #1
Bedrock Linux Advocate
Registered: Nov 2004
Location: the internet
Distribution: Gentoo mostly, currently. Bedrock Linux (+gentoo,+others) again, soon.
Posts: 197

Rep: Reputation: 62
replace filename with variable

what my script needs:

i want to be able to use a filename in my script several times, and i want to use this script hundreds of times for different files, without having to manually change the filename over and over each time i want it run.

so for example, maybe the script is run with*:
$ wabob foo.png
and then the script will be able to run all the things i intend to do, without needing "foo.png" being written in the script, presumably replaced with a "variable"? (sorry if thats the wrong terminology, still early days for me and bash)

so what ways are there of achieving this?

(preferably just using bash... but if something else (C?) is required, very well..)

* i dont mind if i just run it with "wabob", and it prompts me for the filename. either way is good i suppose.

ps, sry if this is something painfully simple i've overlooked while reading about bash.
Old 11-11-2009, 03:39 AM   #2
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,823
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
You can use the positional variable $1
Whatever you pass as the first argument (can be the name of your file) will be assigned to this variable.
You can access it within the script by eg:
ls $1
cp $1 /data/
rm $1

Old 11-12-2009, 12:40 AM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,395

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
You'll find these links educational


bash, script

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
Read filename as a variable kicap Programming 3 08-12-2009 04:27 PM
Storing part of a filename as a variable VTGuitarMan Programming 5 03-12-2008 06:47 AM
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 01:27 PM
Replace variable with user defined variable ce124 Programming 10 04-13-2007 09:29 AM
how to use environment variable as part of filename beeblequix Linux - Newbie 3 07-11-2006 04:55 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:40 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