Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi guys ,
I m developing a script in linux.but getting confused about how would i trim my variable if there is any space.
for example.
I m storing value in a variable named MACHINE_NAME
if MACHINE_NAME=<space><space><space>ABCDF<space><space>
How would i trim it to have my MACHINE_NAME=ABCDF (with no spaces in left or right side.
Distribution: Linux Mint 9 x64, Linux From Scratch
Posts: 46
Rep:
If that doesn't suit your needs, make a separate PERL file in the same folder the trims an argument string and returns a trimmed string (I'm not familiar with PERL, just with Google and a bit of Linux :P) when it is called.
I have got following information from another forum.
The shell automatically removes seemingly excessive whitespaces, which is why you have to double quote variables if you want to preserve them.
Code:
MACHINE_NAME=$( echo $MACHINE_NAME )
This is so simple.
BTW thank you all for your quick replies.
I have got following information from another forum.
The shell automatically removes seemingly excessive whitespaces, which is why you have to double quote variables if you want to preserve them.
Code:
MACHINE_NAME=$( echo $MACHINE_NAME )
This is so simple.
BTW thank you all for your quick replies.
Hi,
which shell do you use? I tried single and double quoting the variable to preserve leading and trailing spaces. In both cases they were not preserved. I am now looking into shopt, maybe some options prevents it.
extended by enabling extended globbing (great name!) using the Shopt Builtin ...
to allow the *(pattern-list) form "to match zero or more occurrences of the given patterns", in this case, zero or more occurrences of a space.
Here's how that looks at the command prompt:
Code:
c@CW8:~$ MACHINE_NAME=' ABCDEF ' # Initialise
c@CW8:~$ shopt -s extglob # Enable extended globbing in pattern matching
c@CW8:~$ MACHINE_NAME=${MACHINE_NAME##*( )} # Strip any and all spaces from the left
c@CW8:~$ echo "'$MACHINE_NAME'" # Showing it has worked
'ABCDEF '
c@CW8:~$ MACHINE_NAME=${MACHINE_NAME%%*( )} # Strip any and all spaces from the right
c@CW8:~$ echo "'$MACHINE_NAME'" # Showing it has worked
'ABCDEF'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.