Can I introspect the type of a bash variable?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I'm writing a function that outputs dates. I'd like to allow the user to customize the output by supplying arguments to date
with an environment variable. To preserve white space in format strings, I'd like to accept args in an array like this:
function argdates
while [ $# -gt 0 ] && date "$DATE_ARGS[@]" -d @$1
do shift
done
The user might want to use an array if they have spaces in a date format string:
DATE_ARGS=( -u "+%y/%U, %I%p %a" )
argdates 1476395008 1493172224
# output:
# 16/41, 09PM Thu
# 17/17, 02AM Wed
But in this case, an array might be overkill:
DATE_ARGS="-u -Iseconds"
argdates 1476395008 1493172224
# output:
# date: invalid option -- ' '
# Try 'date --help' for more information.
# output should be:
# 2016-10-13T21:43:28+00:00
# 2017-04-26T02:03:44+00:00
I don't want to require an array for simple cases like this. Is it possible to tell what type the variable is?
bash environment-variables variable array arguments
add a comment |
up vote
2
down vote
favorite
I'm writing a function that outputs dates. I'd like to allow the user to customize the output by supplying arguments to date
with an environment variable. To preserve white space in format strings, I'd like to accept args in an array like this:
function argdates
while [ $# -gt 0 ] && date "$DATE_ARGS[@]" -d @$1
do shift
done
The user might want to use an array if they have spaces in a date format string:
DATE_ARGS=( -u "+%y/%U, %I%p %a" )
argdates 1476395008 1493172224
# output:
# 16/41, 09PM Thu
# 17/17, 02AM Wed
But in this case, an array might be overkill:
DATE_ARGS="-u -Iseconds"
argdates 1476395008 1493172224
# output:
# date: invalid option -- ' '
# Try 'date --help' for more information.
# output should be:
# 2016-10-13T21:43:28+00:00
# 2017-04-26T02:03:44+00:00
I don't want to require an array for simple cases like this. Is it possible to tell what type the variable is?
bash environment-variables variable array arguments
related unix.stackexchange.com/q/246026/38906
– cuonglm
Apr 13 '17 at 4:25
In the second case, what's so hard about writingDATE_ARGS=(-u -Iseconds)
?
– Barmar
Apr 13 '17 at 6:22
You can't export an array as an environment variable. See stackoverflow.com/questions/5564418/…
– Barmar
Apr 13 '17 at 6:25
@Barmar yes that's basically where I'm at right now: Assume end user is comfortable creating a bash array. But the common way to export arguments for a script to use is the second example, not with an array.
– Travis Wellman
Apr 13 '17 at 6:26
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm writing a function that outputs dates. I'd like to allow the user to customize the output by supplying arguments to date
with an environment variable. To preserve white space in format strings, I'd like to accept args in an array like this:
function argdates
while [ $# -gt 0 ] && date "$DATE_ARGS[@]" -d @$1
do shift
done
The user might want to use an array if they have spaces in a date format string:
DATE_ARGS=( -u "+%y/%U, %I%p %a" )
argdates 1476395008 1493172224
# output:
# 16/41, 09PM Thu
# 17/17, 02AM Wed
But in this case, an array might be overkill:
DATE_ARGS="-u -Iseconds"
argdates 1476395008 1493172224
# output:
# date: invalid option -- ' '
# Try 'date --help' for more information.
# output should be:
# 2016-10-13T21:43:28+00:00
# 2017-04-26T02:03:44+00:00
I don't want to require an array for simple cases like this. Is it possible to tell what type the variable is?
bash environment-variables variable array arguments
I'm writing a function that outputs dates. I'd like to allow the user to customize the output by supplying arguments to date
with an environment variable. To preserve white space in format strings, I'd like to accept args in an array like this:
function argdates
while [ $# -gt 0 ] && date "$DATE_ARGS[@]" -d @$1
do shift
done
The user might want to use an array if they have spaces in a date format string:
DATE_ARGS=( -u "+%y/%U, %I%p %a" )
argdates 1476395008 1493172224
# output:
# 16/41, 09PM Thu
# 17/17, 02AM Wed
But in this case, an array might be overkill:
DATE_ARGS="-u -Iseconds"
argdates 1476395008 1493172224
# output:
# date: invalid option -- ' '
# Try 'date --help' for more information.
# output should be:
# 2016-10-13T21:43:28+00:00
# 2017-04-26T02:03:44+00:00
I don't want to require an array for simple cases like this. Is it possible to tell what type the variable is?
bash environment-variables variable array arguments
bash environment-variables variable array arguments
asked Apr 13 '17 at 3:18
Travis Wellman
12817
12817
related unix.stackexchange.com/q/246026/38906
– cuonglm
Apr 13 '17 at 4:25
In the second case, what's so hard about writingDATE_ARGS=(-u -Iseconds)
?
– Barmar
Apr 13 '17 at 6:22
You can't export an array as an environment variable. See stackoverflow.com/questions/5564418/…
– Barmar
Apr 13 '17 at 6:25
@Barmar yes that's basically where I'm at right now: Assume end user is comfortable creating a bash array. But the common way to export arguments for a script to use is the second example, not with an array.
– Travis Wellman
Apr 13 '17 at 6:26
add a comment |
related unix.stackexchange.com/q/246026/38906
– cuonglm
Apr 13 '17 at 4:25
In the second case, what's so hard about writingDATE_ARGS=(-u -Iseconds)
?
– Barmar
Apr 13 '17 at 6:22
You can't export an array as an environment variable. See stackoverflow.com/questions/5564418/…
– Barmar
Apr 13 '17 at 6:25
@Barmar yes that's basically where I'm at right now: Assume end user is comfortable creating a bash array. But the common way to export arguments for a script to use is the second example, not with an array.
– Travis Wellman
Apr 13 '17 at 6:26
related unix.stackexchange.com/q/246026/38906
– cuonglm
Apr 13 '17 at 4:25
related unix.stackexchange.com/q/246026/38906
– cuonglm
Apr 13 '17 at 4:25
In the second case, what's so hard about writing
DATE_ARGS=(-u -Iseconds)
?– Barmar
Apr 13 '17 at 6:22
In the second case, what's so hard about writing
DATE_ARGS=(-u -Iseconds)
?– Barmar
Apr 13 '17 at 6:22
You can't export an array as an environment variable. See stackoverflow.com/questions/5564418/…
– Barmar
Apr 13 '17 at 6:25
You can't export an array as an environment variable. See stackoverflow.com/questions/5564418/…
– Barmar
Apr 13 '17 at 6:25
@Barmar yes that's basically where I'm at right now: Assume end user is comfortable creating a bash array. But the common way to export arguments for a script to use is the second example, not with an array.
– Travis Wellman
Apr 13 '17 at 6:26
@Barmar yes that's basically where I'm at right now: Assume end user is comfortable creating a bash array. But the common way to export arguments for a script to use is the second example, not with an array.
– Travis Wellman
Apr 13 '17 at 6:26
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
It seems to me that you may want to let your function pass on any command line options directly to GNU date
while treating numerical non-option specially:
argdates () date "$opts[@]" -f -
This bash
function would go through its command line arguments and save them in the opts
array, until it hits an argument which is --
(the standard way of signalling the end of options) or that starts with a digit. Each argument saved into opts
is shifted off of the list of command line arguments.
Once an argument has been found that is not an option to date
, we assume that the rest of the arguments are UNIX epoch timestamps and loop over these, calling date
with our saved options for each timestamp. See the comment in the code for how to do this looping more efficiently.
Example invocations:
$ argdates 1476395008 1493172224
Thu Oct 13 23:43:28 CEST 2016
Wed Apr 26 04:03:44 CEST 2017
$ argdates -- 1476395008 1493172224
Thu Oct 13 23:43:28 CEST 2016
Wed Apr 26 04:03:44 CEST 2017
$ argdates +%D 1476395008 1493172224
10/13/16
04/26/17
$ argdates +%F 1476395008 1493172224
2016-10-13
2017-04-26
$ argdates -u 1476395008 1493172224
Thu Oct 13 21:43:28 UTC 2016
Wed Apr 26 02:03:44 UTC 2017
$ argdates -u -Iseconds 1476395008 1493172224
2016-10-13T21:43:28+00:00
2017-04-26T02:03:44+00:00
add a comment |
up vote
0
down vote
You could do that much easier by giving up on using arrays and letting the user specify DATE_ARGS
simply as a string that should be inserted in the command line of date
:
$ argdates()
for d; do printf %s "$DATE_ARGS"
$ DATE_ARGS='-u "+%y/%U, %I%p %a"' argdates 1476395008 1493172224
16/41, 09PM Thu
17/17, 02AM Wed
$ DATE_ARGS='-u -Iseconds' argdates 1476395008 1493172224
2016-10-13T21:43:28+00:00
2017-04-26T02:03:44+00:00
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
It seems to me that you may want to let your function pass on any command line options directly to GNU date
while treating numerical non-option specially:
argdates () date "$opts[@]" -f -
This bash
function would go through its command line arguments and save them in the opts
array, until it hits an argument which is --
(the standard way of signalling the end of options) or that starts with a digit. Each argument saved into opts
is shifted off of the list of command line arguments.
Once an argument has been found that is not an option to date
, we assume that the rest of the arguments are UNIX epoch timestamps and loop over these, calling date
with our saved options for each timestamp. See the comment in the code for how to do this looping more efficiently.
Example invocations:
$ argdates 1476395008 1493172224
Thu Oct 13 23:43:28 CEST 2016
Wed Apr 26 04:03:44 CEST 2017
$ argdates -- 1476395008 1493172224
Thu Oct 13 23:43:28 CEST 2016
Wed Apr 26 04:03:44 CEST 2017
$ argdates +%D 1476395008 1493172224
10/13/16
04/26/17
$ argdates +%F 1476395008 1493172224
2016-10-13
2017-04-26
$ argdates -u 1476395008 1493172224
Thu Oct 13 21:43:28 UTC 2016
Wed Apr 26 02:03:44 UTC 2017
$ argdates -u -Iseconds 1476395008 1493172224
2016-10-13T21:43:28+00:00
2017-04-26T02:03:44+00:00
add a comment |
up vote
0
down vote
It seems to me that you may want to let your function pass on any command line options directly to GNU date
while treating numerical non-option specially:
argdates () date "$opts[@]" -f -
This bash
function would go through its command line arguments and save them in the opts
array, until it hits an argument which is --
(the standard way of signalling the end of options) or that starts with a digit. Each argument saved into opts
is shifted off of the list of command line arguments.
Once an argument has been found that is not an option to date
, we assume that the rest of the arguments are UNIX epoch timestamps and loop over these, calling date
with our saved options for each timestamp. See the comment in the code for how to do this looping more efficiently.
Example invocations:
$ argdates 1476395008 1493172224
Thu Oct 13 23:43:28 CEST 2016
Wed Apr 26 04:03:44 CEST 2017
$ argdates -- 1476395008 1493172224
Thu Oct 13 23:43:28 CEST 2016
Wed Apr 26 04:03:44 CEST 2017
$ argdates +%D 1476395008 1493172224
10/13/16
04/26/17
$ argdates +%F 1476395008 1493172224
2016-10-13
2017-04-26
$ argdates -u 1476395008 1493172224
Thu Oct 13 21:43:28 UTC 2016
Wed Apr 26 02:03:44 UTC 2017
$ argdates -u -Iseconds 1476395008 1493172224
2016-10-13T21:43:28+00:00
2017-04-26T02:03:44+00:00
add a comment |
up vote
0
down vote
up vote
0
down vote
It seems to me that you may want to let your function pass on any command line options directly to GNU date
while treating numerical non-option specially:
argdates () date "$opts[@]" -f -
This bash
function would go through its command line arguments and save them in the opts
array, until it hits an argument which is --
(the standard way of signalling the end of options) or that starts with a digit. Each argument saved into opts
is shifted off of the list of command line arguments.
Once an argument has been found that is not an option to date
, we assume that the rest of the arguments are UNIX epoch timestamps and loop over these, calling date
with our saved options for each timestamp. See the comment in the code for how to do this looping more efficiently.
Example invocations:
$ argdates 1476395008 1493172224
Thu Oct 13 23:43:28 CEST 2016
Wed Apr 26 04:03:44 CEST 2017
$ argdates -- 1476395008 1493172224
Thu Oct 13 23:43:28 CEST 2016
Wed Apr 26 04:03:44 CEST 2017
$ argdates +%D 1476395008 1493172224
10/13/16
04/26/17
$ argdates +%F 1476395008 1493172224
2016-10-13
2017-04-26
$ argdates -u 1476395008 1493172224
Thu Oct 13 21:43:28 UTC 2016
Wed Apr 26 02:03:44 UTC 2017
$ argdates -u -Iseconds 1476395008 1493172224
2016-10-13T21:43:28+00:00
2017-04-26T02:03:44+00:00
It seems to me that you may want to let your function pass on any command line options directly to GNU date
while treating numerical non-option specially:
argdates () date "$opts[@]" -f -
This bash
function would go through its command line arguments and save them in the opts
array, until it hits an argument which is --
(the standard way of signalling the end of options) or that starts with a digit. Each argument saved into opts
is shifted off of the list of command line arguments.
Once an argument has been found that is not an option to date
, we assume that the rest of the arguments are UNIX epoch timestamps and loop over these, calling date
with our saved options for each timestamp. See the comment in the code for how to do this looping more efficiently.
Example invocations:
$ argdates 1476395008 1493172224
Thu Oct 13 23:43:28 CEST 2016
Wed Apr 26 04:03:44 CEST 2017
$ argdates -- 1476395008 1493172224
Thu Oct 13 23:43:28 CEST 2016
Wed Apr 26 04:03:44 CEST 2017
$ argdates +%D 1476395008 1493172224
10/13/16
04/26/17
$ argdates +%F 1476395008 1493172224
2016-10-13
2017-04-26
$ argdates -u 1476395008 1493172224
Thu Oct 13 21:43:28 UTC 2016
Wed Apr 26 02:03:44 UTC 2017
$ argdates -u -Iseconds 1476395008 1493172224
2016-10-13T21:43:28+00:00
2017-04-26T02:03:44+00:00
edited Nov 25 at 11:43
answered Nov 25 at 11:38
Kusalananda
118k16223361
118k16223361
add a comment |
add a comment |
up vote
0
down vote
You could do that much easier by giving up on using arrays and letting the user specify DATE_ARGS
simply as a string that should be inserted in the command line of date
:
$ argdates()
for d; do printf %s "$DATE_ARGS"
$ DATE_ARGS='-u "+%y/%U, %I%p %a"' argdates 1476395008 1493172224
16/41, 09PM Thu
17/17, 02AM Wed
$ DATE_ARGS='-u -Iseconds' argdates 1476395008 1493172224
2016-10-13T21:43:28+00:00
2017-04-26T02:03:44+00:00
add a comment |
up vote
0
down vote
You could do that much easier by giving up on using arrays and letting the user specify DATE_ARGS
simply as a string that should be inserted in the command line of date
:
$ argdates()
for d; do printf %s "$DATE_ARGS"
$ DATE_ARGS='-u "+%y/%U, %I%p %a"' argdates 1476395008 1493172224
16/41, 09PM Thu
17/17, 02AM Wed
$ DATE_ARGS='-u -Iseconds' argdates 1476395008 1493172224
2016-10-13T21:43:28+00:00
2017-04-26T02:03:44+00:00
add a comment |
up vote
0
down vote
up vote
0
down vote
You could do that much easier by giving up on using arrays and letting the user specify DATE_ARGS
simply as a string that should be inserted in the command line of date
:
$ argdates()
for d; do printf %s "$DATE_ARGS"
$ DATE_ARGS='-u "+%y/%U, %I%p %a"' argdates 1476395008 1493172224
16/41, 09PM Thu
17/17, 02AM Wed
$ DATE_ARGS='-u -Iseconds' argdates 1476395008 1493172224
2016-10-13T21:43:28+00:00
2017-04-26T02:03:44+00:00
You could do that much easier by giving up on using arrays and letting the user specify DATE_ARGS
simply as a string that should be inserted in the command line of date
:
$ argdates()
for d; do printf %s "$DATE_ARGS"
$ DATE_ARGS='-u "+%y/%U, %I%p %a"' argdates 1476395008 1493172224
16/41, 09PM Thu
17/17, 02AM Wed
$ DATE_ARGS='-u -Iseconds' argdates 1476395008 1493172224
2016-10-13T21:43:28+00:00
2017-04-26T02:03:44+00:00
edited Nov 25 at 14:40
answered Nov 25 at 14:33
pizdelect
16114
16114
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f358695%2fcan-i-introspect-the-type-of-a-bash-variable%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
related unix.stackexchange.com/q/246026/38906
– cuonglm
Apr 13 '17 at 4:25
In the second case, what's so hard about writing
DATE_ARGS=(-u -Iseconds)
?– Barmar
Apr 13 '17 at 6:22
You can't export an array as an environment variable. See stackoverflow.com/questions/5564418/…
– Barmar
Apr 13 '17 at 6:25
@Barmar yes that's basically where I'm at right now: Assume end user is comfortable creating a bash array. But the common way to export arguments for a script to use is the second example, not with an array.
– Travis Wellman
Apr 13 '17 at 6:26