Re: bash cut problem?
- From: "Chris F.A. Johnson" <cfajohnson@xxxxxxxxx>
- Date: 23 Sep 2009 23:05:42 GMT
On 2009-09-23, pk wrote:
T wrote:
Hi:
I'm having trouble when piping a variable to the "cut" command:
bash-3.2$ foo=`ssh cc1 date '+%Y%m%d%H%M%S'`
bash-3.2$ echo $foo
20090923151350
bash-3.2$ echo -n $foo | cut -c1
bash-3.2$
First thing to do is to quote the variable, and not use -n:
echo "$foo" | cut -c1
If you want to get the first character, in bash you can also do
echo "${foo:0:1}"
In any POSIX shell:
printf "%s\n" "${foo%"${foo#?}"}"
In bash:
read -n1 fc <<.
$foo
..
printf "%s\n" "$fc"
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.
- Follow-Ups:
- Re: bash cut problem?
- From: Geoff Clare
- Re: bash cut problem?
- References:
- bash cut problem?
- From: T
- Re: bash cut problem?
- From: pk
- bash cut problem?
- Prev by Date: Re: Why preceed a "rm" command with a backslash?
- Next by Date: Re: Why preceed a "rm" command with a backslash?
- Previous by thread: Re: bash cut problem?
- Next by thread: Re: bash cut problem?
- Index(es):
Relevant Pages
|