- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Trim a variable
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2006 08:18 AM
04-06-2006 08:18 AM
Trim a variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2006 08:24 AM
04-06-2006 08:24 AM
Re: Trim a variable
x="ab "
y=${x%% *}
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2006 08:25 AM
04-06-2006 08:25 AM
Re: Trim a variable
Jeff Traigle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2006 08:32 AM
04-06-2006 08:32 AM
Re: Trim a variable
To set a variable to "empty", use this:
# export myvar=1
# echo $myvar
1
# export myvar=
# echo $myvar
#
the "export myvar=" sets myvar to an empty value.
# unset myvar
is deleting the variable.
Hope this helps!
Regards
Torsten.
__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.
__________________________________________________
No support by private messages. Please ask the forum!
If you feel this was helpful please click the KUDOS! thumb below!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2006 08:38 AM
04-06-2006 08:38 AM
Re: Trim a variable
e.g
a=" Hello "
output
"Hello" [ no space before and after]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2006 08:42 AM
04-06-2006 08:42 AM
Re: Trim a variable
y=${a# *}
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2006 08:57 AM
04-06-2006 08:57 AM
Re: Trim a variable
typeset -L b=$a
c=${b%% *}
echo "c is =$c="
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2006 09:06 AM
04-06-2006 09:06 AM
Re: Trim a variable
X=" hello "
echo "Before: \"${X}\""
echo "${X}" | read X
echo "After : \"${X}\""
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2006 10:27 AM
04-06-2006 10:27 AM
Re: Trim a variable
Analogous to Clay's solution for trimming leading and trailing blanks:
#/usr/bin/sh
a=" hello "
echo "Before: [${a}]"
a=`echo ${a}`
echo "After : [${a}]"
Note that the variable is re-assigned to itself as the lvalue of 'echo'ing it.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2006 11:22 PM
04-06-2006 11:22 PM
Re: Trim a variable
a=" hello "
a=$(echo "$a"|tr -d " ")
This removes all spaces in the var non only leading and trailing.
To remove leading and trailing:
a=" h ello "
a=$(echo "$a"|sed 's/^ *//g;s/ *$//g')
HTH,
Art