From b2ac02243beadc0c00fe20bde818b778523dd4aa Mon Sep 17 00:00:00 2001
From: Piotr Grzybowski <merlin@narsil.org.pl>
Date: Wed, 16 Dec 2015 15:25:30 +0100
Subject: [PATCH] $^ special variable

---
 subst.c | 6 ++++++
 test.c  | 7 +++++++
 2 files changed, 13 insertions(+)

diff --git a/subst.c b/subst.c
index 0f6c9ef..b3ade0f 100644
--- a/subst.c
+++ b/subst.c
@@ -141,6 +141,7 @@ SHELL_VAR *ifs_var;
 char *ifs_value;
 unsigned char ifs_cmap[UCHAR_MAX + 1];
 int ifs_is_set, ifs_is_null;
+extern char *last_special_argument;
 
 #if defined (HANDLE_MULTIBYTE)
 unsigned char ifs_firstc[MB_LEN_MAX];
@@ -8247,6 +8248,11 @@ param_expand (string, sindex, quoted, expanded_something,
       temp = itos (number_of_args ());
       break;
 
+    /* $^ -- [ -f /tmp/myfile ] && { echo "$^ is here"; head -1 "$^"; } */
+    case '^':
+      temp=strdup(last_special_argument);
+      break;
+
     /* $? -- return value of the last synchronous command. */
     case '?':
       temp = itos (last_command_exit_value);
diff --git a/test.c b/test.c
index 95e10a0..fa18e74 100644
--- a/test.c
+++ b/test.c
@@ -98,6 +98,8 @@ extern int errno;
 
 #define TEST_ERREXIT_STATUS	2
 
+char *last_special_argument=NULL;
+
 static procenv_t test_exit_buf;
 static int test_error_return;
 #define test_exit(val) \
@@ -548,6 +550,11 @@ unary_test (op, arg)
 	      stat_buf.st_atime <= stat_buf.st_mtime);
 
     case 'f':			/* File is a file? */
+    if (last_special_argument!=NULL) {
+        free(last_special_argument);
+        last_special_argument=NULL;
+    }
+    last_special_argument=strdup(arg);
       if (sh_stat (arg, &stat_buf) < 0)
 	return (FALSE);
 
-- 
2.4.3

