Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 11-17-2010, 11:20 PM   #1
Registered: Jun 2010
Posts: 32

Rep: Reputation: 16
Bash commands in /etc/profile file

Hi guys, this didnt really fit in the programming section so I'll post here. Hopefully a nice easy one.

Basically, I'm trying to work some logic into the /etc/profile file when setting some environment variables based on group membership. The problem I'm having is because this isnt a straight bash script, trying to manipulate a string as I normally would isnt working. (I THINK its because i'm trying to process a local variable from inside the script, outside of the script)

For instance I'm trying to truncate a variable.

The variable value is SYD_ADMIN which I'm trying to shorten to SYD.

Not so hard 'echo $variable |cut -c -3'

But this doesnt seem to work in this /etc/profile file. Im not sure how the command logic goes. Anyone have any ideas? or point me in the right direction.

Or even still, should I be doing this in a completely different way?

Thanks in advance champs!
Old 11-18-2010, 12:17 AM   #2
Registered: Nov 2010
Distribution: Debian Lenny
Posts: 136
Blog Entries: 1

Rep: Reputation: 23
So can you export the variable and make it global?
1 members found this post helpful.
Old 11-18-2010, 12:19 AM   #3
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Originally Posted by mhouston100
For instance I'm trying to truncate a variable.

The variable value is SYD_ADMIN which I'm trying to shorten to SYD.

Not so hard 'echo $variable |cut -c -3'
Are you using single quotes (') or backticks (`) to surround the echo-cut command? You need to use backticks to get command substitution to work--or use the newer form: $()

For example:
trunced_variable=`echo ${variable} | cut -c -3`


trunced_variable=$( echo ${variable} | cut -c -3 )
1 members found this post helpful.
Old 11-18-2010, 12:39 AM   #4
LQ Newbie
Registered: Nov 2010
Posts: 5

Rep: Reputation: 1
Misunderstood the question. And dark helmet already solved it.

I thought you wanted to truncate a variable name. Food for thought though:

What interests me more is why would you want to truncate a variable name?

But as the above post says, all of the bash initialization files - profile, .bashrc - after setting the values export them so that they are available to any shells that spawn out after that.

I don't see a need to truncate a varname but if you have to here's a sample:

Say you want to truncate the variable "$this" to "$is"

$echo $this
$eval export `echo "this" | cut -c3-`=12
$echo $is

This gives you $is in current and any other shells that spawn out of this one.

Hope this helps.

Last edited by crashpoint_zero; 11-18-2010 at 12:41 AM.
1 members found this post helpful.
Old 11-18-2010, 03:21 PM   #5
Registered: Jun 2010
Posts: 32

Original Poster
Rep: Reputation: 16
Whew, must have been a goddamned spelling mistake or something.

I have it sorted now with:

export tmpgrp=`echo $grp |cut -c -3`

So it all seems to be workign the way I want it.

The reason I'm cutting it is that all the users are in groups like syd_admin, syd_service, syd_store etc and I need to assign some variables based on the branch which is always the first 3 characters 'syd'.

Unless I'm doing this the complete wrong way which is entirely probable I'm not exactly a jedi when it comes to scripting, this has just sort of pushed into my lap...

But thanks for all the help guys, it got me sorted.
Old 11-18-2010, 10:16 PM   #6
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
If the system has an /etc/profile.d directory then there are advantages in creating a new file in there for your changes. If not then in creating a new file (say /etc/profile.local) and modify /etc/profile to source it. The advantages are that it is then simple a) to identify the local changes and b) to switch them off for problem investigation.

If you are only running bash (or whatever other shells support it) a faster solution is export tmpgrp=${variable%_*}. It is faster because it does not spawn any child processes while the $( | } and equivalent ` | ` method spawns two.

There is a convention of using uppercase for environment variable names (example: output of env command). If you like that convention you may prefer export TMPGRP=${variable%_*}

Last edited by catkin; 11-18-2010 at 10:17 PM. Reason: B -> b


environment, profile, variable

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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Running bash but common bash commands not working michellepace Linux - Newbie 11 07-17-2010 09:19 AM
Need help about bash profile file. gardenair Linux - Newbie 6 03-05-2010 07:50 AM
[Bash] - sed - read commands from file mrq Programming 4 08-24-2009 06:43 AM
Bash Profile ...... babujd Linux - Newbie 16 07-23-2009 06:25 PM
how to edit file from bash commands legend1079 Linux - Newbie 3 03-03-2004 01:59 PM > Forums > Linux Forums > Linux - General

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